Click here to Skip to main content
15,887,214 members
Articles / Web Development / ASP.NET

Setting up MySql Membership with Visual Studio 2010

Rate me:
Please Sign up or sign in to vote.
4.47/5 (13 votes)
11 Oct 2010CPOL2 min read 70.2K   41   12
Setting up MySql membership provider with .NET Framework 4.0 and VS 2010

Introduction

This article will help in setting up the MySql membership provider with Visual Studio 2010.

Background

Membership provider will help in automatically setting up the login functionality that comes with .NET Framework.

Prerequisites

  • MySql server should be installed on the local machine or on the remote machine with which you have access.
  • We need a database to which we will connect to test the membership provider after configuring.

Following Steps Will Help in Setting Up the MySql Membership Provider with .NET

  • Download and install the fully-managed ADO.NET driver for MySQL from here version 6.3.4+
  • You might have to close down the Visual Studio development env. before starting the install.

    MySqlConnecterSetup.JPG

  • After installation, you can confirm that the MySql assemblies are present in C:\WINDOWS\assembly folder like in the image below:

    MySqlAssembly.JPG

  • Configure a database in MySql server, I have created a "Test" database on localhost, User as "root" and password as "rootpassword", I will be using these same credential in connection strings configuration.
  • Put the following code for connection string in web.config:
    XML
    <connectionStrings>
    <remove name="LocalMySqlServer"/>
       <add name="LocalMySqlServer" 
            connectionString="Datasource=localhost;
                              uid=root;
                              Pwd=rootpassword;
                              Database=test;" 
            providerName="MySql.Data.MySqlClient" />
    </connectionStrings>
  • Put the following code for Membership provider, Profile Provider and Role Provider. Check that you refer to the correct connection string name in the below config.
    XML
    <membership defaultProvider="MySqlMembershipProvider">
    <providers>
    <clear />
      <add name="MySqlMembershipProvider" 
           type="MySql.Web.Security.MySQLMembershipProvider, 
                 MySql.Web,Version=6.3.4.0,
                 Culture=neutral,
                 PublicKeyToken=c5687fc88969c44d" 
           connectionStringName="LocalMySqlServer" 
           enablePasswordRetrieval="false" 
           enablePasswordReset="true" 
           requiresQuestionAndAnswer="false" 
           requiresUniqueEmail="true" 
           passwordFormat="Hashed" 
           maxInvalidPasswordAttempts="5" 
           minRequiredPasswordLength="6" 
           minRequiredNonalphanumericCharacters="0" 
           passwordAttemptWindow="10" 
           applicationName="/" 
           autogenerateschema="true" />
    </providers>
    </membership>
    <profile>
    <providers>
    <clear />
      <add type="MySql.Web.Security.MySQLProfileProvider, 
                 MySql.Web,Version=6.3.4.0,
                 Culture=neutral,
                 PublicKeyToken=c5687fc88969c44d" 
           name="MySqlProfileProvider" 
           applicationName="/" 
           connectionStringName="LocalMySqlServer" 
           autogenerateschema="true" />
    </providers>
    </profile>
    <roleManager enabled="true" defaultProvider="MySqlRoleProvider">
    <providers>
    <clear />
      <add connectionStringName="LocalMySqlServer" 
           applicationName="/" 
           name="MySqlRoleProvider" 
           type="MySql.Web.Security.MySQLRoleProvider,
                 MySql.Web,Version=6.3.4.0,
                 Culture=neutral,PublicKeyToken=c5687fc88969c44d" 
           autogenerateschema="true" />
    </providers>
    </roleManager>
  • Open machine.config from "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG" in the connection string section, add the following code:

    XML
    <connectionStrings>
    <add name="LocalMySqlServer" 
    connectionString="Datasource=localhost;
                      uid=root;
                      Pwd=rootpassword;
                      Database=test;" />
    </connectionStrings>
  • In same machine.config, check and modify the providers for Membership, Profile and Role also check that connectionStringName attribute points to correct connection string name. Don't delete any of the config settings that are present by default in these provider sections, just add the following lines between provider sections.
    XML
    <membership>
    <providers>
      <add name="MySQLMembershipProvider" 
           type="MySql.Web.Security.MySQLMembershipProvider, 
                 MySql.Web, Version=6.3.4.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="" 
           autogenerateschema="true"/>
    </providers>
    </membership>
    <profile>
    <providers>
      <add name="MySQLProfileProvider" 
           type="MySql.Web.Profile.MySQLProfileProvider, 
                 MySql.Web, Version=6.3.4.0,
                 Culture=neutral,
                 PublicKeyToken=c5687fc88969c44d" 
           connectionStringName="LocalMySqlServer" 
           applicationName="/" />
    </providers>
    </profile>
    <roleManager>
    <providers>
     <add name="MySQLRoleProvider" 
          type="MySql.Web.Security.MySQLRoleProvider,
                MySql.Web,
                Version=6.3.4.0,
                Culture=neutral,
         PublicKeyToken=c5687fc88969c44d" 
         connectionStringName="LocalMySqlServer" 
         applicationName="/" />
    </providers>
    </roleManager>
  • Go to Visual Studio, click Project->ASP.NET configuration:

    AspNetConfig.JPG

  • ASP.NET Web Application Administration will open in a new window, click on security tab you can now create User, Roles and Access rules
  • You can also check with the database in MySql that has been configured with the project with the list of *aspnet* tables, these are auto generated in the database.

    tables.JPG

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMySQL Membership Roles Pin
RyanH8814-Oct-14 15:45
RyanH8814-Oct-14 15:45 
Good evening, I've been struggling to set up membership roles for my website. First off, please let me apologize and say that I'm not extremely technical versed, so I apologize if I'm not describing everything in the best way or if I'm going to in depth.

I've been developing a website of mine for quite a while now and feel like one of the final pieces I need to set up is membership roles. I've developed the site using MS Visual Web Developer 2010 Express. The code is in VB. The files are all .aspx. I use MySQL as my database through a hosted solution.

Originally the website would check to see if the user was valid/active by following this code on the login screen:

VB
Conn = ConfigurationManager.ConnectionStrings("MySQL_ODBC").ConnectionString
            OConn = New OdbcConnection(Conn)

                           SQLC = ""
                           SQLC = SQLC & "SELECT UserId, firstname, subactive FROM usertbl"
                           SQLC = SQLC & " WHERE loginid = '" & loginname.text & "'"
                           SQLC = SQLC & " and password = '" & loginpass.text & "'"
                           SQLC = SQLC & ";"

                           OComm = New OdbcCommand(SQLC, OConn)

                           OConn.Open

                           ORead = OComm.ExecuteReader(CommandBehavior.CloseConnection)

                              If ORead.Read()

                                      session("UserIdDB") = ORead("UserId")
                                      session("loginidDB") = loginname.text
                                      session("firstnameDB") = ORead("firstname")
                                      session("activeID") = ORead("subactive")

                                      ORead.Close()
                                      OConn.Close()


The connection string was referenced in the web.config file of the site and would connect to the database on the hosted environment and check all the credentials. If the user login and password checked out then the user would be re-directed to a user menu. I've noticed a lot of instability with this method and at times the site will randomly log out users.

I've been reading about membership roles and hope that the membership roles will help to solve any security issues and provide a more stable environment. At this point I really don't know where to go to make the transition or what is involved.

I found the article you posted online here:
Setting up MySql Membership with Visual Studio 2010[^]
and feel that it has the main components of what I'm trying to accomplish.

However, I'm not really sure of how to implement all of this or how it will work with my current site. I need a little more guidance through this. I've been struggling to get this implemented with my current site and would really appreciate any further insight you could help me with.

Please help me to determine what specific steps I would need to take to set up all the appropriate roles, tables, code, etc. to implement everything in my current site functionality.

I really appreciate any of your insight and time.

Thank you,

Ryan
Questiona question Pin
keyHu6-Jan-14 4:38
keyHu6-Jan-14 4:38 
AnswerRe: a question Pin
deepaktripathi6-Jan-14 8:59
deepaktripathi6-Jan-14 8:59 
Questionthanks a lot Pin
keyHu6-Jan-14 4:13
keyHu6-Jan-14 4:13 
QuestionWeb Site Administration Tool - Error Pin
Radek.Szymanski10-Dec-12 9:12
Radek.Szymanski10-Dec-12 9:12 
QuestionPublicKeyToken Pin
nasuru829-Oct-12 20:27
nasuru829-Oct-12 20:27 
GeneralMy vote of 5 Pin
meltamps22-Feb-12 15:32
meltamps22-Feb-12 15:32 
GeneralUnable to initialize provider. Missing or incorrect schema. Mysql Provider Pin
kokomantequilla26-Nov-10 23:11
kokomantequilla26-Nov-10 23:11 
GeneralRe: Unable to initialize provider. Missing or incorrect schema. Mysql Provider Pin
deepaktripathi16-Dec-10 1:48
deepaktripathi16-Dec-10 1:48 
GeneralMy vote of 4 Pin
tonym00112-Nov-10 10:07
tonym00112-Nov-10 10:07 
AnswerRe: My vote of 4 Pin
deepaktripathi16-Nov-10 0:13
deepaktripathi16-Nov-10 0:13 
GeneralThanks for sharing Pin
Hemant.Kamalakar13-Oct-10 23:43
Hemant.Kamalakar13-Oct-10 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.