简体   繁体   English

ASP.Net MVC SimpleMembershipProvider和使用角色

[英]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. 我想在控制器操作上使用[Authorize] ,只允许特定角色的选定人员访问控制器。

Example: 例:

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

Prior to ASP.Net 4 , I could add roles like this: ASP.Net 4之前,我可以添加以下角色:

 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. 我得到了UserProfile表,但仍想使用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? 现在,我是否需要编写自己的查询以将角色添加到上表中,并且还添加了如何使用角色,并使它们链接到以下位置的[Authorize(Roles = "Admin, User")]控方?

Thanks. 谢谢。

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")]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM