Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Simple Membership Provider with mysql

Is it possible to use Simple Membership Provider with mysql(also using Entity Framework)

If yes, how to achieve it.

Any documentation/link is really appreciated

Thank in advance

like image 680
Dranix Avatar asked Sep 27 '12 12:09

Dranix


3 Answers

I create MySqlSimpleMembershipProvider on GitHub and make sample projects.

https://github.com/xyz37/MySqlSimpleMembershipProvider

That's all.

Thank you for the answer to this question.

Click to sample screenshot : https://raw.github.com/xyz37/MySqlSimpleMembershipProvider/master/_ScreenShot/SimpleMembershipProviderTest.png

like image 134
Kim Ki Won Avatar answered Sep 27 '22 22:09

Kim Ki Won


Check this brief article from Fabio Costa on how to use SimpleMembership and OAuth with mySQL and bigint or any database and datatype you want: http://fabiocosta.ca/2012/11/24/use-simplemembership-and-oauth-with-any-database-and-datatype/. It should solve your problem. Good luck.

I also did following changes to my code:

  • Added references to MySql.Data, MySql.Data.Entity and MySql.Web. Modified property 'Copy Local' to true on each reference.

  • Changed connectionstring and added DbProviderFactory in web.config to enable MySql connections.

  • Added roleManager and memberShip configuration in web.config.

web.config

<connectionStrings>    
    <add name="DefaultConnection" connectionString="Data Source=localhost;Database=dbname;User ID=username;Password=password;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<system.data>
    <DbProviderFactories>
        <clear />
        <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.6.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>
<system.web>
    <roleManager enabled="true" defaultProvider="simple">
        <providers>
            <clear/>
            <add name="simple" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
        </providers>
    </roleManager>
    <membership defaultProvider="simple">
        <providers>
            <clear/>
            <add name="simple" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"/>
        </providers>
    </membership>
</system.web>
  • Created _AppStart.cshtml in root to initialize db connection and autom. create tables in db upon application startup.

_AppStart.cshtml

@{
    if (!WebSecurity.Initialized)
    {
        WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
    }
}
  • Added filter to ensure db connection during authentication requests. This is the same as manually placing the InitializeSimpleMembership attribute on each controller.

App_Start\FilterConfig.cs

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new InitializeSimpleMembershipAttribute());
    }
}
  • Then got my oAuth fixed with facebook following this tutorial: http://www.asp.net/mvc/tutorials/mvc-4/using-oauth-providers-with-mvc
like image 35
ssanchezz23 Avatar answered Sep 27 '22 22:09

ssanchezz23


Unfortunately, no.

SimpleMembershipProvider is ultimately just a derived class of the good, old MembershipProvider that we all love and hate.

It's there because people didn't like just how many abstract functions you'd have to override to get a custom MembershipProvider working. So this is a very much dumbed-down implementation of MembershipProvider. You'd have to go back re-override the implementations for these functions with SimpleMembershipProvider.

I had the same question about modifying the DB design, had to go back to a custom MembershipProvider class.

Also, taken from Microsoft employee/ASP.NET developer John Galloway's blog:

While SimpleMembership is not database agnostic, it works across the SQL Server family. It continues to support full SQL Server, but it also works with SQL Azure, SQL Server CE, SQL Server Express, and LocalDB. Everything's implemented as SQL calls rather than requiring stored procedures, views, agents, and change notifications.

Note that SimpleMembership still requires some flavor of SQL Server - it won't work with MySQL, NoSQL databases, etc. You can take a look at the code in WebMatrix.WebData.dll using a tool like ILSpy if you'd like to see why - there are places where SQL Server specific SQL statements are being executed, especially when creating and initializing tables. It seems like you might be able to work with another database if you created the tables separately, but I haven't tried it and it's not supported at this point.

like image 22
Mahmoud Al-Qudsi Avatar answered Sep 27 '22 21:09

Mahmoud Al-Qudsi