问题
I have read a ton of different posts and tutorials and couldn't find an answer to this specific need.
I'd like to use the SimpleMembership
provider in my ASP.NET MVC app, but I would like to turn the Role provider off (OAuth too since I will not be using it). That means that I would like the SMP to create only webpages_Membership
table on initialization, not the tables for oauth or roles.
Note that I don't want to turn off the SimpleMembership, only the roles and the oauth support.
Question: Can such a scenario be achieved by configuration?
Attempt:
I've tried by setting this in web.config
(each where it belongs):
< add key="enableSimpleMembership" value="false" />
< roleManager enabled="false" >
but then the InitializeSimpleMembershipAttribute
breaks on LazyInitializer.EnsureInitialized
with:
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.
which is completely true, but that's exactly what I want. Is there some way to tell the SimpleMembership not to use the Role Provider at all, while still retaining other features of WebSecurity's such as login other membership stuff?
回答1:
The Simple Membership API is not a loosely coupled architecture so it all comes in the WebSecurity assembly.The role manager will be instantiated lazily and the option of using it or not completely lies with you. As long as you're not using UserInRole methods, the role provider is not instantiated. In effect there is no loss in performance. Thus you can just go ahead and use Simple Membership without using the role provider without worrying about turning it off.
回答2:
Here, a dummy role provider:
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");
}
}
来源:https://stackoverflow.com/questions/20760610/turning-off-only-the-role-provider-in-simplemembership