leora
leora

Reputation: 196781

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

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.

Upvotes: 11

Views: 19160

Answers (4)

Durlove Roy
Durlove Roy

Reputation: 217

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();
    }

Upvotes: 0

Matthew Jones
Matthew Jones

Reputation: 26190

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

I use these all the time.

Upvotes: 3

BigBlondeViking
BigBlondeViking

Reputation: 3963

Easy~

HttpContext.Current.User.IsInRole("roleName")

Upvotes: 8

Chris Van Opstal
Chris Van Opstal

Reputation: 37577

if (User.IsInRole("rolename")) {
  // my action
}

Upvotes: 23

Related Questions