如何为 ASP.NET MVC 2 创建自定义成员资格提供程序?
如何基于 ASP.NET 成员资格提供程序为 ASP.NET MVC 2 创建自定义成员资格?
How do I create a custom membership for ASP.NET MVC 2 based on the ASP.NET membership provider?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我创建了一个包含自定义成员资格提供程序的新项目,并覆盖了
MembershipProvider
抽象类中的ValidateUser
方法:然后,我通过以下方式将该提供程序连接到我的 ASP.NET MVC 2 项目:添加引用并从 web.config 中指出它:
我确实需要创建一个继承
RoleProvider
抽象类并重写GetRolesForUser
方法的自定义类。ASP.NET MVC 授权使用该方法来查找分配给当前登录用户的角色,并确保允许该用户访问控制器操作。
以下是我们需要执行的步骤:
1) 创建一个继承 RoleProvider 抽象类并重写 GetRolesForUser 方法的自定义类:
2) 通过 web.config 将角色提供程序与 ASP.NET MVC 2 应用程序连接:
3) 设置想要的控制器/操作上方的 Authorize(Roles="xxx,yyy") :
就是这样!现在可以了!
4) 可选:设置自定义
Authorize
属性,以便我们可以将不需要的角色重定向到 AccessDenied 页面:现在我们可以使用我们自己制作的属性将用户重定向到访问被拒绝的视图:
就是这样!
超级骗子!
以下是我用来获取所有这些信息的一些链接:
自定义角色提供程序:
http://davidhayden.com/blog/dave/archive/2007 /10/17/CreateCustomRoleProviderASPNETRolePermissionsSecurity.aspx
我希望此信息有帮助!
I have created a new project containing a custom membership provider and overrode the
ValidateUser
method from theMembershipProvider
abstract class:Then I connected that provider to my ASP.NET MVC 2 project by adding a reference and pointing it out from my web.config:
I do need to create a custom class that inherits the
RoleProvider
abstract class and overrides theGetRolesForUser
method.The ASP.NET MVC Authorizing uses that method to find out which roles are assigned to the current logged-on user and makes sure the user is permitted to access the controller action.
Here are the steps we need to take:
1) Create a custom class that inherits the RoleProvider abstract class and overrides the GetRolesForUser method:
2) Connect the role provider with the ASP.NET MVC 2 application via our web.config:
3) Set the Authorize(Roles="xxx,yyy") above the wanted Controller / Action:
That's it! Now it works!
4) Optional: set a custom
Authorize
attribute so we can redirect an unwanted role to an AccessDenied Page:Now we can use our own made attribute to redirect our users to access denied view:
That's it!
Super duper!
Here are some of the links I've used to get all this info:
Custom role provider:
http://davidhayden.com/blog/dave/archive/2007/10/17/CreateCustomRoleProviderASPNETRolePermissionsSecurity.aspx
I hope this info helps!
这对我有用
http://mattwrock.com/post/2009/10/14/Implementing-custom-Membership-Provider-and-Role-Provider-for-Authinticating-ASPNET-MVC-Applications.aspx
This worked for me
http://mattwrock.com/post/2009/10/14/Implementing-custom-Membership-Provider-and-Role-Provider-for-Authinticating-ASPNET-MVC-Applications.aspx
也可以使用更少的代码来使用此方法,我不完全确定此方法是否安全,但适用于您使用的任何数据库。
在 global.asax 中,
它的作用是从由 FormsAuthenticationTicket 生成的 authCookie 中读取角色
,登录逻辑如下所示
我将角色存储在数据库中,有两个表: table: Role ,其中包含以下列: roleID 和roleName 和表:UsersRoles 包含列:userID 和 roleID,这使得多个用户可以拥有多个角色,并且可以轻松创建自己的逻辑来添加/删除用户的角色等。例如,这使您可以使用 [Authorize(Roles="Super Admin")]。希望这有帮助。
编辑:忘记进行密码检查,但您只需在 logOn 方法中添加一个 if 来检查提供的用户名和密码是否检查,如果没有检查则返回 false
Its also possible to use this with a much smaller amount of code, i'm not entirely sure if this method is as safe but works very well with any database you use.
in the global.asax
what this does is that it reads the roles from the authCookie which was made from FormsAuthenticationTicket
and the logon logic looks like this
i store the roles in my database with two tables: table: Role which has the columns: roleID and roleName and the table: UsersRoles wich has the columns: userID and roleID, this makes it possible for multiple roles for several users and it's easy to make your own logic to add/remove roles from users and so forth. This enables you to use [Authorize(Roles="Super Admin")] for instance. hope this helps.
edit: forgot to make the password check but you just add an if in the logOn method which checks if the username and password provided checks up and if not it returns false
我使用 NauckIt.PostgreSQL 提供程序的源代码作为基础,并对其进行修改以满足我的需要。
I used the NauckIt.PostgreSQL provider's source code as a base, and modified it to suit my needs.