Optionally provide private feedback to help us improve this article...

Thank you for your feedback!


Working with the InstantForum User Object

The code below shows how to populate the InstantForum user object from a web site using ASP.NET forms authentication. It also demonstrates how to get the roles (known as member groups within InstantForum) associated with a forum user account.

private void Page_Load(System.Object sender, System.EventArgs e)
{
	// simple check to see if authenticated user belongs to the administrator role
	if (System.Web.HttpContext.Current.User.IsInRole("Administrators")) {
		Response.Write("You are an administrator!");
	} else {
		Response.Write("You are NOT an administrator!");
	}

	// simple check to see if authenticated user belongs to the moderator role
	if (System.Web.HttpContext.Current.User.IsInRole(InstantASP.Common.Business.Roles.SelectRole(InstantASP.Common.Enumerations.EnumRequiredRoles.Moderator).RoleName)) {
		System.Web.HttpContext.Current.Response.Write("You are a moderator!");
	} else {
		System.Web.HttpContext.Current.Response.Write("You are NOT a moderator!");
	}

	// local user object 
	InstantASP.InstantForum.Components.User user = null;

	// list all roles associated with a user
	// are we authenticated?
	if (Request.IsAuthenticated) {

		// render text
		Response.Write("You are associated to the following roles:");

		// if so attempt to create forum user object from generic principal object
		user = InstantASP.InstantForum.Business.User.SelectUser(System.Web.HttpContext.Current.User.Identity);

		// loop through roles associated with user rendering to screen
		foreach (InstantASP.Common.Components.UserRole role in user.Roles) {
			Response.Write(role.RoleID.ToString() + ", " + role.RoleName);
		}

	}

	// determine the users primary role within InstantForum.NET
	// are we authenticated?
	if (Request.IsAuthenticated) {

		// if so attempt to create forum user object from generic principal object
		User = InstantASP.InstantForum.Business.User.SelectUser(System.Web.HttpContext.Current.User.Identity);

		// render primary role
		Response.Write("You're primary role is: ");
		Response.Write(user.Roles.PrimaryRole.RoleName);

	}

}

User.IsInRole

The InstantASP.InstantForum.Components.User object also contains a IsInRole method that allows you to check if a user belongs to a specific role. For example...

If (user.IsInRole("Administrator")) 
{
      // we are an administator
}

User.IsInSubscriptionPlan

You can also check to see if the user belongs to any of the paid subscription plans you can associate with users within InstantForum. For example...

If (user.IsInSubscriptionPlan("Gold Subscriber")) 
{
      // we are a member of the gold subscribers
}