【问题标题】:Turning off only the Role Provider in SimpleMembership仅关闭 SimpleMembership 中的角色提供程序
【发布时间】:2013-12-24 12:03:13
【问题描述】:

我已经阅读了大量不同的帖子和教程,但找不到这个特定需求的答案。

我想在我的 ASP.NET MVC 应用程序中使用 SimpleMembership 提供程序,但我想关闭角色提供程序(OAuth 也是,因为我不会使用它) .这意味着我希望 SMP 在初始化时仅创建 webpages_Membership 表,而不是用于 oauth 或角色的表。

注意,我不想关闭 SimpleMembership,只有角色和 oauth 支持。

问题:这样的场景可以通过配置实现吗?

尝试: 我尝试在web.config 中设置它(每个都属于它):

< add key="enableSimpleMembership" value="false" />

< roleManager enabled="false" >

但随后InitializeSimpleMembershipAttributeLazyInitializer.EnsureInitialized 上中断:

Exception has been thrown by the target of an invocation.
| 
└> The ASP.NET Simple Membership database could not be initialized
   |
   └> The Role Manager feature has not been enabled.

这是完全正确的,但这正是我想要的。有什么方法可以告诉 SimpleMembership 根本不使用 Role Provider,同时仍保留 WebSecurity 的其他功能,例如登录其他会员资格?

【问题讨论】:

    标签: asp.net-mvc-4 web-config simplemembership roleprovider


    【解决方案1】:

    Simple Membership API 不是一个松散耦合的架构,因此它全部来自 WebSecurity 程序集。角色管理器将被延迟实例化,使用或不使用它的选择完全取决于您。只要您不使用 UserInRole 方法,就不会实例化角色提供程序。实际上没有性能损失。 因此,您可以继续使用 Simple Membership,而无需使用角色提供程序,而不必担心将其关闭。

    【讨论】:

    • 但是如何防止它在数据库中创建表呢?我知道的唯一方法(在另一个项目上做过)是创建一个自定义角色提供程序。在这种情况下,没有创建表。
    【解决方案2】:

    这里是一个虚拟角色提供者:

    公共类 DummyRoleProvider : System.Web.Security.RoleProvider { 私有字符串_ApplicationName = null;

        public DummyRoleProvider ()
        {
            return;
        }
    
        public override string ApplicationName
        {
            get
            {
                return _ApplicationName;
            }
            set
            {
                _ApplicationName = value;
            }
        }
    
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            base.Initialize(name, config);
        }
    
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new Exception("NOT IMPLEMENTED");
        }
    
        public override void CreateRole(string roleName)
        {
            throw new Exception("NOT IMPLEMENTED");
        }
    
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new Exception("NOT IMPLEMENTED");
        }
    
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new Exception("NOT IMPLEMENTED");
        }
    
        public override string[] GetAllRoles()
        {
            throw new Exception("NOT IMPLEMENTED");
        }
    
        public override string[] GetRolesForUser(string username)
        {
            throw new Exception("NOT IMPLEMENTED");
        }
    
        public override string[] GetUsersInRole(string roleName)
        {
            throw new Exception("NOT IMPLEMENTED");
        }
    
        public override bool IsUserInRole(string username, string roleName)
        {
            throw new Exception("NOT IMPLEMENTED");
        }
    
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new Exception("NOT IMPLEMENTED");
        }
    
        public override bool RoleExists(string roleName)
        {
            throw new Exception("NOT IMPLEMENTED");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      相关资源
      最近更新 更多