eba
eba

Reputation: 979

asp mvc 2 roles and membership

i implemented custom role and membersip providers. and i have question, when user get roles??? i have something like:

if (provider.ValidateUser(model.UserName, model.Password))
            {
            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
            }

that works. But then, in some controller, i use:

if (!User.IsInRole("user"))
            return RedirectToAction("Index", "Home");

and it always false, like user do not have any roles.

so finally, when user gets this roles, and where they are stored????

Upvotes: 0

Views: 320

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

The IsInRole method queries the role provider to determine if the currently logged in user has the role. When you create a new ASP.NET MVC application using the default template it uses a SQL role provider meaning that it stores the roles in the database. So when you create a new user you can assign him roles and when you use User.IsInRole it will query the database. Here's a blog post which describes in more details.

Upvotes: 2

Related Questions