我需要对用户进行身份验证和授权的最低 ASP.NET 提供程序实现是什么?
默认情况下,ASP.NET MVC 将 AccountController 设置为使用 SqlMembershipProvider、SqlProfileProvider 和 SqlRoleProvider。我真的不需要所有的东西,事实上,将我的数据塑造成该模型更麻烦。
为了获得身份验证和授权并且不破坏可能存在的其他依赖关系,我至少需要在 MembershipProvider、RoleProvider 和 ProfileProvider 抽象类上实现什么?
例如,在 ProfileProvider 上,它希望我重写“FindInactiveProfilesByUserName”方法,但我并不真正关心此功能。当 NotImplementedException 触发时,我的应用程序将在哪里中断?
此外,例如在 MembershipProvider 上,我不需要 FindUsersByEmail 方法。如果我不实现它,ASP.NET MVC 会在某个时刻卡住吗?如果是的话,在哪里?
By default ASP.NET MVC setups up the AccountController to use the SqlMembershipProvider, SqlProfileProvider and the SqlRoleProvider. I don't really need everything that brings to the table, in fact, it is more of a hassle to shape my data into that model.
What is the minimum I need to implement on the MembershipProvider, RoleProvider and ProfileProvider abstract classes to get authentication and authorization and not break some other dependency that might be there?
For instance, on the ProfileProvider it wants me to override the "FindInactiveProfilesByUserName" method, but I don't really care about this feature. Where is my app going to break when the NotImplementedException fires?
Additionally, on the MembershipProvider for instance, I don't need the FindUsersByEmail method. If I don't implement it will ASP.NET MVC choke at some point? If so, where?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,ASP.NET MVC 在身份验证方面并没有真正为您做任何事情。考虑到这一点,正如 @chrispr 所说,您应该只需要实现 ValidateUser ,并且 ASP.NET MVC 项目模板创建的项目仅在身份验证期间调用该方法。
关于授权,我查看了Reflector中的
AuthorizationAttribute
,发现它调用了IPrincipal.IsInRole
。查看 Reflector 中的System.Web.Security.RolePrincipal
,IsInRole
调用GetRolesForUser
,因此您可以尝试仅实现该方法来开始。我出于类似的原因实现了自定义提供程序(我不喜欢 sql 提供程序使用的架构),但我选择不实现自定义配置文件提供程序,因为它似乎依赖于配置文件属性的配置设置,并且我不希望走这条路(请参阅ASP.NET 配置文件属性概述)。
附带说明一下,我发现在实现自己的提供程序时,查看 Reflector 中的
SqlMembershipProvider
和SqlRoleProvider
很有帮助,因此您可能也想这样做。As far as I know, ASP.NET MVC doesn't really do anything for you with regard to authentication. With that in mind, as @chrispr says, you should only need to implement
ValidateUser
, and the project created by the ASP.NET MVC project template only calls that method during authentication.Regarding authorization, I took a look at
AuthorizationAttribute
in Reflector and found that it callsIPrincipal.IsInRole
. Looking atSystem.Web.Security.RolePrincipal
in Reflector,IsInRole
callsGetRolesForUser
, so you could try implementing only that method to start with.I implemented custom providers for similar reasons (I don't like the schema the sql providers use), but I chose not to implement a custom profile provider since it seems to rely on configuration settings for the profile properties, and I didn't want to go that route (see ASP.NET Profile Properties Overview).
As a side note, I found that looking at the
SqlMembershipProvider
andSqlRoleProvider
in Reflector was helpful when I implemented my own providers, so you might want to do the same.我相信您只需要在 MembershipProvider 上实现 ValidateUser 即可利用 MembershipProvider 的身份验证功能。其余功能由提供的 Web 控件(如 CreateUserWizard)调用,因此请确保在使用这些控件时禁用这些控件上任何不受支持的功能。至于其余的(RoleProvider 和 ProfileProvider),如果您不使用任何与用户角色或用户配置文件相关的功能,则不必实现任何成员。
I believe you only need to implement ValidateUser on the MembershipProvider to take advantage of the authentication features of the MembershipProvider. The rest of the features are invoked by provided web controls like CreateUserWizard, so make sure to disable any of the unsupported features on those controls when using them. As for the rest (RoleProvider and ProfileProvider), if you're not using any of the features related to User Roles or User Profiles, you don't have to implement any of the members.
这是我的自定义提供程序中的内容:
以及:
Here's what I have in my custom providers:
and: