Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DefaultConnection and membership - what is the connection between localsqlserver and defaultconnection

Hmmmm... I really can't get my head around this.

In web.config I have:

<connectionStrings>
   <clear /> <!--- need this to prevent using LocalSqlServer from machine.config or somewhere becouse it is not present when when publish to hosting -->
   <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-Goaly-20120615111028;Integrated Security=SSPI" />  
</connectionStrings>

and

<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <!-- I think this line is telling asp.net that I want the membership working agains defaultconnection, and not LocalSqlServer connection????? -->
    <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>

But then it is complaining:

The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty.

When I use the publish from VS2012 RC in the settings tab I get a ModelContext and a DefaultConnection, so how is this supposed to work?

What I want is simply to get the membership tables in the same database as the tables created by modelcontext.

This should be so oob, but I must have overcomplicated it some how.

Thanks for any help.

Regards

Larsi

like image 441
Larsi Avatar asked Dec 20 '22 18:12

Larsi


2 Answers

The default settings for all web sites on a windows machines inherit the settings of 2 configuration files: machine.config and web.config, which can be found in the directory Windows/Microsoft.NET/Framework(or Framework64 for 64 bit)/version/config.

Here is a snippet from the default version of machine.config:

<system.web>
    <processModel autoConfig="true"/>
    <httpHandlers/>
    <membership>
        <providers>
            <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=""/>
        </providers>
    </membership>
    <profile>
        <providers>
            <add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </providers>
    </profile>
    <roleManager>
        <providers>
            <add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
            <add name="AspNetWindowsTokenRoleProvider" applicationName="/" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </providers>
    </roleManager>
</system.web>

So the answer to your question is: it is machine.config that establishes the dependency to the LocalSqlServer connection. When you add the <clear/> tag as the other answers to this question suggest, you are removing the reference to the membership provider named AspNetSqlMembershipProvier, and with it the reference to LocalSqlServer.

like image 80
Paul Keister Avatar answered Jan 05 '23 00:01

Paul Keister


It is mentioned in the follwing post forumsasp that adding </clear> tag before your connection string in the membership element and rolemangaer element ( if present in your web.config) will solves the issue.

<membership defaultProvider="DefaultMembershipProvider">
  <providers>
   <clear/> <!-- solves the issue -->    
   <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>

also you can use SqlMetal.exe to add membership tables to your existing database.

like image 25
Waqar Janjua Avatar answered Jan 04 '23 23:01

Waqar Janjua