I have been noodling with MVC3 / cshtml / Razor, and I put together a web site and tried to deploy it. I was using the WebSecurity that was nicely integrated into the default web site that I started with, and was delighted about how easy it was to implement. With a few hours of work, I had a site that was performing registration, adding users to my database, emailing for confirmation and I didn’t write any of that code! Sweet. Let’s deploy. Then, ugh, an error.
Server Error in ‘/’ Application.
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: The connection name ‘LocalSqlServer’ was not found in the applications configuration or the connection string is empty.
Source Error:
Line 238: <membership> Line 239: <providers> Line 240: <add name=”AspNetSqlMembershipProvider” type=”System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” connectionStringName=”LocalSqlServer” enablePasswordRetrieval=”false” enablePasswordReset=”true” requiresQuestionAndAnswer=”true” applicationName=”/” requiresUniqueEmail=”false” passwordFormat=”Hashed” maxInvalidPasswordAttempts=”5″ minRequiredPasswordLength=”7″ minRequiredNonalphanumericCharacters=”1″ passwordAttemptWindow=”10″ passwordStrengthRegularExpression=”” /> Line 241: <add name=”MySQLMembershipProvider” type=”MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.3.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d” connectionStringName=”LocalMySqlServer” enablePasswordRetrieval=”false” enablePasswordReset=”true” requiresQuestionAndAnswer=”true” applicationName=”/” requiresUniqueEmail=”false” passwordFormat=”Clear” maxInvalidPasswordAttempts=”5″ minRequiredPasswordLength=”7″ minRequiredNonalphanumericCharacters=”1″ passwordAttemptWindow=”10″ passwordStrengthRegularExpression=”” /> Line 242: </providers> |
Source File: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config Line: 240
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
I started immediately looking for an entry possibly added by WebMatrix. But there was none referencing AspNetSqlMembershipProvider, or LocalSqlServer. Then I noticed that this reference was in the machine.config of the server. Well, this is not my server, so I contacted support to ask them about it. They promptly and courteously told me that they did not solve programming questions. OK, on my own – no big surprise there. But I thought it might be a normal “gotcha” that lots of people run into. Well, it is… Here is what needs to happen:
First the reference is partially right. You DO need to have this reference in place, however the machine config does not know where your site has put the Membership info. So you have to replace it. Use this:
<connectionStrings>
<clear />
<add name=”myDB” providerName=”System.Data.SqlClient”
connectionString=”Data Source=someserver.com;Initial Catalog=myDB;User ID=myDB_user;Password=’xxxxx’;Integrated Security=False;” />
<remove name=”LocalSqlServer” />
<add name=”LocalSqlServer” providerName=”System.Data.SqlClient”
connectionString=”Data Source=someserver.com;Initial Catalog=myDB;User ID=myDB_user;Password=’xxxxx’;Integrated Security=False;” />
</connectionStrings>
Of course replace the database details with your own.
Once this was in place, I ran it again and got the following error:
Server Error in ‘/’ Application.
The Role Manager feature has not been enabled.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Configuration.Provider.ProviderException: The Role Manager feature has not been enabled.
Source Error:
Line 3: var providerName = “System.Data.SqlClient”; Line 4: //var db = Database.OpenConnectionString(connectionString, providerName); Line 5: WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection (connectionString, providerName, Line 6: “UserProfile”, “UserId”, “Email”, true); Line 7: |
Source File: e:\web\myWeb\_AppStart.cshtml Line: 5
It is now looking at my (mind you WORKING LOCALLY 🙂 ) init and saying it is failing. To be honest, since this is new tech for me, I wasn’t sure if I should be enabling the role manager, or if I had enabled something that was adding a new / different security setting that would cause some conflict. With a bit more research (Googling WebSecurity and Role Manager), I saw that the WebSecurity uses the Role Manager in “SimpleMembership” mode. OK, so this is needed and probably ok.
But I don’t need the AspNetSqlMembershipProvider… So I removed it (with the <clear/> below):
Inside the System.Web section of my web.config:
<membership defaultProvider=”SimpleMembershipProvider”>
<providers >
<clear/>
<add name=”SimpleMembershipProvider” type=”WebMatrix.WebData.SimpleMembershipProvider,WebMatrix.WebData”/>
</providers>
</membership>
Then I got the error:
Server Error in ‘/’ Application.
Default Membership Provider must be specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Configuration.Provider.ProviderException: Default Membership Provider must be specified.
With a little more digging, I found that this entry in the appSettings section will take care of that issue.
<add key=”enableSimpleMembership” value=”true” />
I hope this helps someone make a few less round trips searching!
Peace…
Helpful Links for this issue:
http://stackoverflow.com/questions/3874279/the-role-manager-feature-has-not-been-enabled
http://forums.asp.net/t/1695156.aspx/1?WebSecurity+in+MVC
https://forums.iis.net/p/1188577/2018786.aspx