asp.net - MVC4 中的 SimpleMembershipProvider 在初始化时在方法上抛出 NotSupportedException

标签 asp.net asp.net-mvc-4 simplemembership

我正在使用 ASP.net MVC4 模板的内置 SimpleMembership,并希望创建一个管理面板来重置用户群的密码和类似操作。

我使用 SecurityGuard.MVC4 nuget 作为起点,并且在大多数操作中都收到 NotSupportedException。 SecurityGuard 使用 Controller 的 Membership 属性来调用 SimpleMembershipProvider 上的 GetAllUsers()、GetUser() 和 ResetPassword() 等方法,它们都会抛出 NotSupportedException。从文档看来,这是设计使然:

SimpleMembershipProvider.GetAllUsers

Note: If the SimpleMembershipProvider class has been initialized using a call to the WebSecurity.InitializeDatabaseConnection() method, this method is not supported and will throw a NotSupportedException exception. However, if the WebSecurity.InitializeDatabaseConnection() method has not been called, and if you have configured your site to use the standard ASP.NET membership provider, this method is passed through to the standard membership provider.

我错过了什么?看来我需要在使用 WebSecurity 来处理像 WebSecurity.CreateUser() 这样的标准内容和直接使用提供程序之间进行选择?

我可以解决这个问题,但我主要困惑的是为什么我不能两者兼得?

最佳答案

SimpleMembership 被有意设计为……嗯,简单。 WebSecurity 使用 SimpleMembership,并使用 SimpleMembership 派生自的 ExtendedMembershipProvider 类。

本质上,SimpleMembership 不会实现 WebSecurity 中不存在的任何功能,而 GetAllUsers 就是其中之一。是的,这是设计使然。

您有多种选择。所有这些都是解决方法。

  1. 您只需执行自己的查询即可获取所有用户。
  2. 您可以创建自己的从 SimpleMembership 派生的成员(member)资格提供程序并实现这些功能
  3. 您可以返回 SqlMembershipProvider(或通用提供程序)(但避开 WebSecurity)

关于asp.net - MVC4 中的 SimpleMembershipProvider 在初始化时在方法上抛出 NotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12819183/

相关文章:

asp.net-mvc - 使用基本身份验证将现有成员资格/角色提供者添加到新的 WebAPI

entity-framework - 简单成员资格提供程序 - Entity Framework 和角色

c# - 如何解决 FileSystemException : Cannot retrieve length of file, path ='...' (OS Error : No such file or directory, errno = 2)

asp.net - 如何使用 CSS 设置 asp.net 菜单的样式

c# - 在 MVC Razor 中回发后保持下拉选择状态?

c# - 当最后一项匹配时循环列表<string>

asp.net-mvc - 如何使用 WebSecurity + SimpleMembership 手动设置 "log a user in"?

c# - 限制 HttpModule 只处理某些请求

javascript - 除非在网格上单击下一行,否则值不会绑定(bind)到下拉提供程序

c# - 我的母版页使用我所有其他模型继承的 View 模型,如何简化它?