仅关闭SimpleMembership中的角色提供程序

时间:2013-12-24 12:03:13

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

我已经阅读了大量不同的帖子和教程,无法找到满足这一特定需求的答案。

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

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

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

尝试: 我试过在web.config(每个它所属的地方)设置它:

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

< roleManager enabled="false" >

InitializeSimpleMembershipAttribute LazyInitializer.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.

{{1}}

这是完全正确的,但这正是我想要的。有没有办法告诉SimpleMembership根本不使用角色提供者,同时仍保留WebSecurity的其他功能,例如登录其他会员资格?

2 个答案:

答案 0 :(得分:1)

简单成员资格API不是一个松散耦合的体系结构,因此它都出现在WebSecurity程序集中。角色管理器将被懒惰地实例化,并且使用它的选项完全取决于您。只要您不使用UserInRole方法,就不会实例化角色提供程序。实际上,性能没有损失。 因此,您可以在不使用角色提供程序的情况下继续使用简单成员资格,而无需担心将其关闭。

答案 1 :(得分:0)

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

public class DummyRoleProvider:System.Web.Security.RoleProvider     {         private string _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");
    }
}