Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Membership/Role providers for MySQL?

I am not at all familiar with ASP.NET membership/roles. This is my first time using it, and my first time trying ASP.NET MVC. When I create my first project for MVC, it gives me a lovely template to create an account. I was excited to see that I did not have to do this manually. However, it failed because it cannot connect to SQL Server. I do not have SQL Server, I have MySQL. Is there any easy way I can get this system to use MySQL instead, or will I have to create my own authentication?

like image 202
Josh Stodola Avatar asked Jan 10 '10 22:01

Josh Stodola


People also ask

What is the provider name for MySQL?

MySqlClient is for MySql. The type in this registration is the assembly-qualified name of the provider type that derives from MySql. Data. MySqlClient.

Can ASP.NET work with MySQL?

. NET Core and MySQL are both free and open source technologies. The new ASP.NET Core can run on Linux and in Linux Containers, and MySQL is one of the easiest databases to get started with. This makes the combination of ASP.NET Core and MySQL a pretty compelling combination.

What is asp net membership provider?

The ASP.NET membership provider is a feature that enables ASP.NET developers to create Web sites that allow users to create unique user name and password combinations. With this facility, any user can establish an account with the site, and sign in for exclusive access to the site and its services.

Does Visual Studio 2022 support MySQL?

MySQL isn't supported for Visual Studio 2022 yet. it's weird and frustrating, to be honest, but if you want to work with MySQL with VS22, you either need to change the Database or go back to VS19.


2 Answers

Got it figured out! Using version 6.2.2.0 of MySql Connector/Net, follow these steps...

  • Add reference to MySql.Web.dll
  • Change your <membership> in web.config to this:

    <membership defaultProvider="MySqlMembershipProvider">
      <providers>
        <clear/>
        <add name="MySqlMembershipProvider"
             type="MySql.Web.Security.MySQLMembershipProvider,
                   MySql.Web, Version=6.2.2.0, Culture=neutral,
                   PublicKeyToken=c5687fc88969c44d"
             autogenerateschema="true"
             connectionStringName="NAME_OF_YOUR_CONN_STRING"
             enablePasswordRetrieval="false"
             enablePasswordReset="true"
             requiresQuestionAndAnswer="false"
             requiresUniqueEmail="false"
             passwordFormat="Hashed"
             maxInvalidPasswordAttempts="5"
             minRequiredPasswordLength="6"
             minRequiredNonalphanumericCharacters="0"
             passwordAttemptWindow="10"
             passwordStrengthRegularExpression=""
             applicationName="/"
         />
      </providers>
    </membership>
  • Run the Project | ASP.NET Configuration tool and click on the Security tab to test
  • Tested on ASP.NET 3.5, MySQL Server version 5.1, Windows XP 64-bit
like image 117
Josh Stodola Avatar answered Nov 01 '22 05:11

Josh Stodola


The original question didn't specify which version of ASP.NET & MVC was used. With the recent release of .NET 4.5 and MVC 4 I hit the same problem as the OP but with the new technologies. This is my quick fix for it mostly the same config as Josh Stodola's answer but with some additional steps.

<membership defaultProvider="MySqlMembershipProvider">
  <providers>
    <clear />
    <add name="MySqlMembershipProvider"
         type="MySql.Web.Security.MySQLMembershipProvider,
         MySql.Web, Version=6.5.4.0, PublicKeyToken=c5687fc88969c44d"
     autogenerateschema="true"
     connectionStringName="*NAME_OF_YOUR_CONN_STRING*"
     enablePasswordRetrieval="false"
     enablePasswordReset="true"
     requiresQuestionAndAnswer="false"
     requiresUniqueEmail="false"
     passwordFormat="Hashed"
     maxInvalidPasswordAttempts="5"
     minRequiredPasswordLength="6"
     minRequiredNonalphanumericCharacters="0"
     passwordAttemptWindow="10"
     passwordStrengthRegularExpression=""
     applicationName="/" />
  </providers>
</membership>

Get the AccountController and Views working:

  1. Delete the MVC 4 AccountController, AccountModels, Account view folder and _LoginPartial shared view
  2. Create a new MVC 3 web application
  3. Copy the MVC 3 AccountController, AccountModels, Account view folder and _LogOnPartial shared view into your MVC 4 application
  4. Replace @Html.Partial(“_LoginPartial”) in the shared _Layout view with @Html.Partial(“_LogOnPartial”)
like image 39
agilejoshua Avatar answered Nov 01 '22 04:11

agilejoshua