views:

599

answers:

3

what is the code for determining if a user is in a role. I have setup all the users through the ASP.Net configuration Security tab but now want to put logic around some key areas so only people in certain roles can see and access these areas.

+7  A: 
if (User.IsInRole("rolename")) {
  // my action
}
Chris Pebble
what namespace is user in?
ooo
User is a property of the Page and HttpContext classes, so you can access it on the page simply as User, or in non-page file as HttpContext.Current.User. More info at MSDN: http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user.aspx
Chris Pebble
A: 

Check out the Roles class, specifically IsUserInRole, GetUsersInRole, AddUserToRole, etc.

I use these all the time.

Matthew Jones
+1  A: 

Easy~

HttpContext.Current.User.IsInRole("roleName")
BigBlondeViking
this code will looks into the sql database (thats my provider) to determine role in asp.net?
ooo
Yes, it will look into whatever provider you have configured.
Chris Pebble
Gotta love the providers :)
BigBlondeViking