1
votes

When i'm trying to get to Web Site Administration Tool (WAT) (Project->ASP.NET Configuration in Visual Studio) i get following error

You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other >method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in >the root of your site.

And this is my connection string:

<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Database1.mdf;User Instance=true" providerName="System.Data.SqlClient" />

I've also enabled simple membership

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

My roleshipprivider config looks like this

<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
  <providers>
    <clear />
    <add connectionStringName="DefaultConnection" applicationName="/"
      name="MySqlRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
  </providers>
</roleManager>

And my membership conf

 <membership defaultProvider="MyOwnSqlMembershipProvider">
  <providers>
    <clear/>
    <add connectionStringName="DefaultConnection" enablePasswordRetrieval="false"
      enablePasswordReset="true" requiresQuestionAndAnswer="false"
      requiresUniqueEmail="true" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
      minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
      name="MyOwnSqlMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
  </providers>
</membership>

Does anybody know where's the bug?

Thanks in advance, Mateusz Urban

2

2 Answers

3
votes

You should add the code below to Global.aspx.cs inside protected void Application_Start() and should appear at the top before any other registrations. This way, it will always be Initialized before an other operations.

if (!WebSecurity.Initialized)
            WebSecurity.InitializeDatabaseConnection("DefaultConnection", 
"UserProfile", "UserId", "UserName", autoCreateTables: true);
1
votes

You need to include code to actually initialize the membership provider. The following in _AppStart.cshtml should work:

@{   
if (!WebSecurity.Initialized)
{
    WebSecurity.InitializeDatabaseConnection("dbContext", "Users", "Id", "Login", autoCreateTables: false); 
}
}