Skip to content

Latest commit

 

History

History
206 lines (159 loc) · 6.11 KB

README.md

File metadata and controls

206 lines (159 loc) · 6.11 KB

MySql SimpleMembership Provider for ASP.NET MVC 4

with Entity Framework 5.x CodeFirst

License

Apache License 2.0

Libraries

  • MySql.Data.Extension : MySql Entity Framework Extension for Entity Framework Code First
  • MySql.Web.Extension : MySql SimpleMembership Provider for Entity Framework Code First

Samples

  • SimpleMembershipTest : ASP.NET MVC 4 Simple Membership sample web site
  • SimpleMembership.Dac : SimpleMembership Data access control

MySql.Data.Extension Use only

Install package by NuGet Package management

  • Entity Framework 5.x above
  • MySql.Net 6.6.4 above
In app.config
<connectionStrings>
  <add name="SimpleMembershipTestDbContext"
		 connectionString="server=localhost;port=3306;database=SimpleMembershipTest;User Id=dev;Password=thePassword;Persist Security Info=True;"
		 providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<entityFramework>
	<defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data" />
</entityFramework>
<system.data>
	<DbProviderFactories>
		<remove invariant="MySql.Data.MySqlClient" />
		<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>
DbContext class inherited MySqlDbContext
public class SimpleMembershipTestDbContext : MySqlDbContext
{
		public SimpleMembershipTestDbContext()
			: base("SimpleMembershipTestDbContext")
		{
		}
}
Grant privileges to user

mysql --user=root -p

grant all privileges on SimpleMembershipTest.* to 'dev'@'%' identified by 'thePassword' with grant option;
-- grant all privileges on <Database name>.* to '<UserName>'@'%' identified by '<UserPassword>' with grant option;

flush privileges;

use mysql;

-- verify results
select host, user from user where user = 'dev';
select host, user, db from db where user = 'dev';

MySql.Web.Extension Use

Step 1.

Install package by NuGet Package management

  • Entity Framework 5.x above
  • MySql.Net 6.6.4 above
  • Microsoft ASP.NET Razor 2
  • Microsoft ASP.NET Web Pages 2
  • Microsoft ASP.NET Web Pages 2 Data
  • Microsoft ASP.NET Web Pages 2 Web Data
  • Microsoft.Web.Infrastructure

Step 2.

Reference MySql.Data.Extension project

Step 3.

Grant privilege to user

In web.config
<appSettings>
	<add key="enableMySqlSimpleMembership" value="true" />
	<add key="mySqlSecurityInheritedContextType" value="SimpleMembershipTest.Dac.SimpleMembershipTestDbContext, SimpleMembershipTest.Dac" />
</appSettings>
  • "mySqlSecurityInheritedContextType" is System.Type.Name and non argument public constructor need.

see [SimpleMembershipTestDbContext.cs](https://github.com/xyz37/MySqlSimpleMembershipProvider/blob/master/SimpleMembershipTest.Dac/SimpleMembershipTestDbContext.cs)
<connectionStrings>
  <add name="SimpleMembershipTestDbContext"
		 connectionString="server=localhost;port=3306;database=SimpleMembershipTest;User Id=dev;Password=thePassword;Persist Security Info=True;"
		 providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<system.web>
	<membership defaultProvider="MySqlSimpleMembershipProvider">
		<providers>
			<clear />
			<add name="MySqlSimpleMembershipProvider"
				 type="MySql.Web.Security.MySqlSimpleMembershipProvider, MySql.Web.Extension" />
		</providers>
	</membership>

	<roleManager enabled="true" defaultProvider="MySqlSimpleRoleProvider">
		<providers>
			<clear />
			<add name="MySqlSimpleRoleProvider"
				 type="MySql.Web.Security.MySqlSimpleRoleProvider, MySql.Web.Extension" />
		</providers>
	</roleManager>
</system.web>
<entityFramework>
	<defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data" />
</entityFramework>
<system.data>
	<DbProviderFactories>
		<remove invariant="MySql.Data.MySqlClient" />
		<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>
DbContext class inherited MySecuritySqlDbContext

Inherited MySqlSecurityDbContext class that generate below tables.

  • UserProfile
  • WebPages_Membership
  • WebPages_OAuthMembership
  • WebPages_OAuthToken
  • WebPages_Rroles
  • WebPages_UsersInRoles
  • RoleMemberships
public class SimpleMembershipTestDbContext : MySqlSecurityDbContext
{
		public SimpleMembershipTestDbContext()
			: base("SimpleMembershipTestDbContext")
		{
		}
}

Sample Project Screenshot

Add UserProperties
  • Email
  • Facebook
  • Age
  • Rate
  • LastName
  • FirstName
Support External login

SimpleMembership Provider Test Page

Special thanks