----------
20190218
在 ASP.NET ASP.NET Identity 2.0 架構下實作 SQL Server store.
步驟:
- Clone repo and open project in VS with Update 2 installed
在 Visual sturio 2015 版本中開啟專案檔案.
- Build project to restore packages and build project
重建專案
- In the solution, add a new one ASP.NET project with MVC and Individual Authentication
加入 ASP.NET MVC 網站授權方式選擇 Authentications
- Uninstall Microsoft.AspNet.Identity.EntityFramework package from the web application
解除安裝 ASP.NET MVC 網站專案的 Microsoft.AspNet.Identity.EntityFramework 元件.
加入參考 LibMySQL 專案後, 再將所有
using Microsoft.AspNet.Identity.EntityFramework;
的程式改為
using AspNet.Identity.MySQL;
清單為:
IdentityConfig.cs
IdentityModel.cs
Manage.aspx.cs
Startup.Auth.cs
- Update connection string to use the MySQL database as needed
Change connection to SQL Server New database DBAspNet1.
將 Web.config 修改 connection string 為
- In the IdentityModel.cs, let ApplicationUser class extend from Identity user in AspNet.Identity.MySQL
將 IdentityModel.cs 中 ApplicationUser 類別原繼承的 IdentityUser (Microsoft.AspNet.Identity.EntityFramework), 改為繼承 AspNet.Identity.MySQL.IdentityUser.
本步驟在前面改為 using AspNet.Identity.MySQL; 已完成.
- ApplicationDbContext extend from MySqlDatabase and the contructor take a single parameter with the connectionstring name
將 IdentityModel.cs 中 ApplicationDbContext 物件
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
改為
public class ApplicationDbContext : MySQLDatabase
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
- In the ApplicationManager.Create method, replace instantiating UserManager as shown below
var manager = new ApplicationUserManager(new UserStore(context.Get() as MySQLDatabase));
將 IdentityConfig.cs, ApplicationUserManager.Create() 方法中, 改變建立 UserManager 的方式如下:
原為: var manager = new ApplicationUserManager(new UserStore(context.Get()));
改為: var manager = new ApplicationUserManager(new UserStore(context.Get() as MySQLDatabase));
- If any properties are added to the ApplicationUser class, then update the Insert, GetUserByName, GetUserById and Update methods in AspNet.Identity.MySQL project
----------
WebAuth1
Test user:
test1@some.com, aaBB11!!
test2@some.com, aaBB22!!
test3@some.com, aaBB33!!