Search code examples
asp.net-mvc-4web-configsimplemembershiproleprovider

Turning off only the Role Provider in SimpleMembership


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?


Solution

  • 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.