Mark
Mark

Reputation: 7818

ASP.Net MVC SimpleMembershipProvider and using Roles

I would like to user the [Authorize] on my controller actions, to only let selected people in specific roles access the controller.

Example:

[Authorize(Roles = "Admin, User")]

Prior to ASP.Net 4, I could add roles like this:

 if (!Roles.RoleExists("Admin"))
 {
      Roles.CreateRole("Admin");
 }

The above code doesn't add the role to the Roles table.

However, now I only have the following tables:

UserProfile
webpages_Membership
webpages_OAuthMembership
webpages_Roles
webpages_UsersInRoles

I get the UserProfile table, but would still like to use Roles. Do I now have to write my own queries to add roles to the table above, and also to but how do you work with the Roles, and get them to link in to the [Authorize(Roles = "Admin, User")] on the conroller?

Thanks.

Upvotes: 0

Views: 319

Answers (1)

Lotok
Lotok

Reputation: 4607

You can still do it, just use a provider.

 var rolesProvider = (SimpleRoleProvider)Roles.Provider;
 if (!rolesProvider.RoleExists("Admin"))
 {
     rolesProvider.CreateRole("Admin");
 }

The way you use them hasn't changed. What you wrote will work fine

 [Authorize(Roles = "Admin, User")]

Upvotes: 5

Related Questions