WebSecurity.InitializeDatabaseConnection fails with "The Role Manager feature has not been enabled." when called from console program

StackOverflow https://stackoverflow.com/questions/19766603

  •  03-07-2022
  •  | 
  •  

Question

I have a MVC4 application using SimpleMembership to authenticate users.

I want to add users from a console program.

The console program that references a class library that has the method that will do the user creation.

It looks like this:

public class UserBuilder
{
    private static readonly SimpleMembershipInitializer _membershipInitializer;
    private static readonly bool _isInitialized;
    private static readonly object _initializerLock = new object();

    static UserBuilder()
    {
        LazyInitializer.EnsureInitialized(ref _membershipInitializer, ref _isInitialized, ref _initializerLock);
    }

    public void HandleEvent(UserAdded @event)
    {
        if (!WebSecurity.UserExists("ReportModels"))
        {
            WebSecurity.CreateUserAndAccount("ReportModels", "ReportModels");
        };
    }

    private class SimpleMembershipInitializer
    {
        public SimpleMembershipInitializer()
        {
            WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
        }
    }
}

When I start my console application I get System.Configuration.Provider.ProviderException {"The Role Manager feature has not been enabled."} at the line starting with WebSecurity.InitializeDatabaseConnection.

What do I need to do to accomplish this?

I've tried:

  • adding the nuget package Microsoft ASP.NET Web Pages 2 Web Data to both the console project and the class library project.
  • the answers listed in this post: SimpleMembershipProvider not working.
  • verified the connection string.
  • verified that the tables are in place in the database.
  • verified that creating users and authenticating them from the MVC4 project works.
Was it helpful?

Solution

Finally solved it thanks to information found in this blog post: http://insomniacgeek.com/to-call-this-method-the-membership-provider-property-must-be-an-instance-of-extendedmembershipprovider/ and some googling.

In essence I needed to add this to my app.config file:

<system.web>
<profile defaultProvider="SimpleProfileProvider">
  <providers>
    <add name="SimpleProfileProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"
        connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</profile>
<membership defaultProvider="SimpleMembershipProvider">
  <providers>
    <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
  </providers>
</membership>
<roleManager defaultProvider="SimpleRoleProvider" enabled="true">
  <providers>
    <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
  </providers>
</roleManager>
</system.web>

Please note the enabled="true" on the roleManager element. Without that the same exception will be thrown.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top