asp.net membership - how to determine programmatically is user is in role
Asked Answered
C

4

11

What is the code for determining if a user is in a role?

I have set up 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.

Creswell answered 31/7, 2009 at 17:56 Comment(0)
C
23
if (User.IsInRole("rolename")) {
  // my action
}
Corticosterone answered 31/7, 2009 at 17:57 Comment(1)
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: msdn.microsoft.com/en-us/library/…Corticosterone
E
8

Easy~

HttpContext.Current.User.IsInRole("roleName")
Erin answered 31/7, 2009 at 17:58 Comment(2)
this code will looks into the sql database (thats my provider) to determine role in asp.net?Creswell
Yes, it will look into whatever provider you have configured.Corticosterone
T
3

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

I use these all the time.

Tenorrhaphy answered 31/7, 2009 at 17:58 Comment(0)
S
0

thanks to "Chris Van Opstal". i solved my problem like this way,

    public ActionResult Index()
    {

        if (User.IsInRole("Supervisor"))
        {
            return RedirectToAction("Index", "InvitationS");
        }
        return View();
    }
Socha answered 3/7, 2014 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.