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 finall开发者_如何学编程y, when user gets this roles, and where they are stored????
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.
精彩评论