ASP.NET MVC - 角色提供程序的替代方案?
我试图避免使用角色提供程序和成员资格提供程序,因为在我看来它太笨拙了,因此我试图制作自己的“版本”,它不那么笨拙并且更易于管理/灵活。现在我的问题是……除了角色提供者之外,还有其他不错的选择吗? (我知道我可以自定义角色提供者、成员资格提供者等)
通过更易于管理/灵活,我的意思是我仅限于使用角色静态类,而不是直接实现到与数据库上下文交互的服务层中我必须使用 Roles 静态类,它有自己的数据库上下文等,表名也很糟糕..
提前致谢。
I'm trying to avoid the use of the Role Provider and Membership Provider since its way too clumsy in my opinion, and therefore I'm trying to making my own "version" which is less clumsy and more manageable/flexible. Now is my question.. is there an alternative to the Role Provider which is decent? (I know that I can do custom Role provier, membership provider etc.)
By more manageable/flexible I mean that I'm limited to use the Roles static class and not implement directly into my service layer which interact with the database context, instead I'm bound to use the Roles static class which has its own database context etc, also the table names is awful..
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我和你的情况一样——我一直讨厌 RoleProviders。是的,如果您想启动并运行一个小型网站,它们非常棒,但它们不太现实。我一直发现的主要缺点是它们将您直接与 ASP.NET 联系在一起。
我最近的一个项目的方式是定义几个作为服务层一部分的接口(注意:我简化了这些接口 - 但您可以轻松添加到它们):
然后您的用户可以拥有一个角色 枚举:
对于您的
IAuthenticationService
,您可以有一个执行标准密码检查的基本实现,然后您可以有一个FormsAuthenticationService
执行更多操作,例如设置 cookie 等。对于您的AuthorizationService
,您需要这样的东西:在这些基本服务之上,您可以轻松添加服务来重置密码等。
由于您使用的是 MVC,您可以使用
ActionFilter
在操作级别进行授权:然后您可以在控制器操作上进行装饰:
这种方法的优点是您还可以使用依赖项注入和 IoC 容器来连接事物。此外,您还可以在多个应用程序(不仅仅是 ASP.NET 应用程序)中使用它。您将使用 ORM 来定义适当的模式。
如果您需要有关
FormsAuthorization/Authentication
服务的更多详细信息或从这里开始,请告诉我。编辑:要添加“安全修剪”,您可以使用 HtmlHelper 来完成。这可能需要更多……但你明白了。
然后在您的视图中(此处使用 Razor 语法):
编辑:
UserSession
看起来像这样:这样,我们就不会公开当前会话中的密码哈希和所有其他详细信息用户,因为用户的会话生命周期确实不需要它们。
I'm in the same boat as you - I've always hated the RoleProviders. Yeah, they're great if you want to get things up and running for a small website, but they're not very realistic. The major downside I've always found is that they tie you directly to ASP.NET.
The way I went for a recent project was defining a couple of interfaces that are part of the service layer (NOTE: I simplified these quite a bit - but you could easily add to them):
Then your users could have a
Roles
enum:For your
IAuthenticationService
, you could have a base implementation that does standard password checking and then you could have aFormsAuthenticationService
that does a little bit more such as setting the cookie etc. For yourAuthorizationService
, you'd need something like this:On top of these base services, you could easily add services to reset passwords etc.
Since you're using MVC, you could do authorization at the action level using an
ActionFilter
:Which you can then decorate on your controller actions:
The advantage of this approach is you can also use dependency injection and an IoC container to wire things up. Also, you can use it across multiple applications (not just your ASP.NET one). You would use your ORM to define the appropriate schema.
If you need more details around the
FormsAuthorization/Authentication
services or where to go from here, let me know.EDIT: To add "security trimming", you could do it with an HtmlHelper. This probably needs a little more... but you get the idea.
And then inside your view (using Razor syntax here):
EDIT: The
UserSession
would look something like this:This way, we don't expose the password hash and all other details inside the session of the current user since they're really not needed for the user's session lifetime.
我已经实现了一个基于@TheCloudlessSky 帖子的角色提供者。我认为我可以添加和分享我所做的一些事情。
首先,如果您想将
RequirepPermission
类用作操作过滤器的属性,则需要为RequirepPermission
类实现ActionFilterAttribute
类。接口类
IAuthenticationService
和IAuthorizationService
FormsAuthenticationService
类UserSession
calss还有一点是
FormsAuthorizationService
类和我们如何将用户分配给httpContext.Session["CurrentUser"]
。在这种情况下,我的方法是创建 userSession 类的新实例,并将用户从 httpContext.User.Identity.Name 直接分配给 userSession 变量,如您在 FormsAuthorizationService 中看到的那样> 类。然后在您的控制器中,用户通过身份验证后,您可以从数据库获取角色并将其分配给角色会话:
用户从系统注销后,您可以清除会话
该模型中的警告是,当用户登录系统后,如果为用户分配了角色,则授权不起作用,除非他注销并重新登录系统。
另一件事是,不需要为角色创建单独的类,因为我们可以直接从数据库获取角色并将其设置到控制器中的角色会话中。
完成所有这些代码的实现后,最后一步是将此属性绑定到控制器中的方法:
I have implemented a role provider based on @TheCloudlessSky post here. There are few things that I thought I can add and share what I have done.
First if you want to use the
RequirepPermission
class for your action filters as an attribute you need to implementActionFilterAttribute
class forRequirepPermission
class.Interface classes
IAuthenticationService
andIAuthorizationService
FormsAuthenticationService
classUserSession
calssAnother point is
FormsAuthorizationService
class and how we can assign a user to thehttpContext.Session["CurrentUser"]
. My Approach in this situation is to create a new instance of userSession class and directly assign the user fromhttpContext.User.Identity.Name
to the userSession variable as you can see inFormsAuthorizationService
class.then in your controller after the user is authenticated you can get roles from the database and assign it to the roles session:
After the user is logged out of the system you can clear the session
The caveat in this model is that, when the user is signed into the system, if a role is assigned to the user, authorization doesn't work unless he logs out and logs back in the system.
Another thing is that there is no need to have a separate class for roles, since we can get roles directly from database and set it into roles session in a controller.
After you are done with implementing all these codes one last step is to bind this attribute to your methods in your controller:
如果您使用 Castle Windsor 依赖注入,您可以注入 RoleProvider 列表,这些列表可用于从您选择实现的任何源确定用户权限。
http://ivida.co.uk/2011/05/18/mvc-getting-user-roles-from-multiple-sources-register-and-resolve-arrays- of-dependencis-using-the- Fluent-api/
If you use Castle Windsor Dependency Injection you can inject lists of RoleProviders that can be used to ascertain user rights from any source you choose to implement.
http://ivida.co.uk/2011/05/18/mvc-getting-user-roles-from-multiple-sources-register-and-resolve-arrays-of-dependencis-using-the-fluent-api/
您不需要为角色使用静态类。例如, SqlRoleProvider 允许您定义数据库中的角色。
当然,如果您想从自己的服务层检索角色,那么创建自己的角色提供程序并不难 - 实际上没有那么多方法可以实现。
You don't need to use a static class for roles. For instance, the SqlRoleProvider allows you to define the roles in a database.
Of course, if you want to retrieve roles from your own service layer, it's not that hard to create your own role provider - there really aren't that many methods to implement.
您可以实施自己的会员和角色 通过覆盖适当的接口来提供者。
如果您想从头开始,通常这些类型的东西是作为 自定义 http 实现的module 将用户凭据存储在 httpcontext 或会话中。无论哪种方式,您可能都希望使用某种身份验证令牌来设置 cookie。
You can implement your own membership and role providers by overriding the appropriate interfaces.
If you want to start from scratch, typically these types of things are implemented as a custom http module which stores the users credentials either in the httpcontext or the session. Either way you'll probably want to set a cookie with some sort of authentication token.