Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up existing membership with mvc4

I have an existing SQL membership db that I used with webforms, I am trying set it up to work with mvc4 but with no luck, when I try to get user by id(I know this user exists) I get null exception.And web I open web app config I can clearly see it has no members or roles..etc.

Here is part of config from my web froms app :

<configuration>
  <connectionStrings>
    <add name="ApplicationServices" connectionString="Data Source=myserver;Initial Catalog=mydb;User ID=myid;Password=mypwd" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
    <membership>
      <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
      <properties>
        <add name="UrgentPoints" type="System.Int32" defaultValue="0" />
      </properties>
    </profile>
    <roleManager enabled="true">
      <providers>
        <clear />
        <add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
        <add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
      </providers>
    </roleManager>
....

And here is for mvc:

<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=myserver;Initial Catalog=mydb;User ID=myid;Password=mypwd" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="true" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <profile defaultProvider="DefaultProfileProvider">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </profile>
    <membership>
      <providers>
        <add connectionStringName="DefaultConnection" enablePasswordRetrieval="false"
          enablePasswordReset="true" requiresQuestionAndAnswer="false"
          requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
          minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
          applicationName="/" name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </providers>
    </membership>
    <roleManager>
      <providers>
        <add connectionStringName="DefaultConnection" applicationName="/"
          name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </providers>
    </roleManager>
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </sessionState>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
like image 910
formatc Avatar asked Mar 26 '12 18:03

formatc


4 Answers

Here is my way after 3 days Googling. Using MVC4 RC, .net Framework 4.5, SQL Server Express with new SqlMemberDB created by .NET 4 cause old SqlMemberDB missing some stored procedure.

  1. add in one more setting to <AppSetting> as below to disable "simple membership":

    <add key="enableSimpleMembership" value="false" />
    
  2. add in database connection as required. I'm using SQL Server 2000, so disable user instance.

    <add name="ScqMember_Context" 
         connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MemberDB;User ID=xxx;Password=xxx;Integrated Security=False;User Instance=False;MultipleActiveResultSets=False" 
         providerName="System.Data.SqlClient" />
    
  3. copy setting configuration from machine.ini under v4 .NET framework.

    <membership>
        <providers>
            <clear/>
            <add name="AspNetSqlMembershipProvider" 
                 type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
                 connectionStringName="ScqMember_Context" 
                 enablePasswordRetrieval="false" enablePasswordReset="true" 
                 requiresQuestionAndAnswer="true" applicationName="/" 
                 requiresUniqueEmail="false" passwordFormat="Hashed" 
                 maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" 
                 minRequiredNonalphanumericCharacters="1" 
                 passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
        </providers>
    </membership>
    <profile>
        <providers>
            <clear/>
            <add name="AspNetSqlProfileProvider" 
                 connectionStringName="ScqMember_Context" 
                 applicationName="/" 
                 type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </providers>
    </profile>
    <roleManager enabled="true">
        <providers>
            <clear />
            <add connectionStringName="ScqMember_Context" 
                 applicationName="/"
                 name="AspNetSqlRoleProvider" 
                 type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </providers>
    </roleManager>
    
  4. Change Login() from Account Controller as below

     /// aspnetSQLMember Authentication
     if (ModelState.IsValid)
     {
         if (Membership.ValidateUser(model.UserName, model.Password))
         {
             FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
    
             if (Url.IsLocalUrl(returnUrl))
             {
                 return Redirect(returnUrl);
             }
             else
             {
                 return RedirectToAction("Index", "Home");
             }
         }
         else
         {
             ModelState.AddModelError("", "The user name or password provided is incorrect.");
         }
     }
    
  5. Update Logoff module from AccountController as below

    /// aspnetSQLMember Authentication
    FormsAuthentication.SignOut();
    

Thanks,

like image 174
Thant Zin Avatar answered Oct 31 '22 15:10

Thant Zin


First be sure to switch to Asp.Net Universal Providers (nuget package) and then be sure to connect to the same DB as the old application. The schema for the asp.net db hasn't changed and should work fine with MVC 4.

like image 29
Israel Lot Avatar answered Oct 31 '22 14:10

Israel Lot


I've been having the same problem - pointing the MVC4 application to an "old" aspnet database. Existing users/passwords are not able to be retrieved under the "default" settings (System.Web.Providers.DefaultProfileProvider) -> note these passwords are encrypted and the appropriate settings are present in the local web.config (under system.web)

<machineKey
  validationKey= "A3..."
  decryptionKey= "8B..."
  validation="SHA1"
  decryption="AES"/>

If I add a new user with the "DefaultProfileProvider" that user can be reconciled and authenticated - it just doesn't work with existing users that were added with the "old" provider methinks.

I haven't had the chance to "switch" to the old but I would suspect that it works (thanks user1010609) @Israel - It is odd and it should work - but it doesn't ;)

like image 1
adelpreore Avatar answered Oct 31 '22 16:10

adelpreore


Remember that you must to include in the references of your project the library: System.Web.Providers.dll

like image 1
Juan Carlos Velez Avatar answered Oct 31 '22 15:10

Juan Carlos Velez