在 ASP.NET ASP.NET Identity 2.0 架構下實作 MYSQL store. Steps to run project 步驟 - 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 元件. UninstallEntityFramework1.jpg UninstallEntityFramework2.jpg 加入參考 LibMySQL 及 ZLib 專案後, 再將所有包含 using Microsoft.AspNet.Identity.EntityFramework; 的程式改為 using LibSQL.DIdentity; 清單如下: App_Start\IdentityConfig.cs App_Start\Startup.Auth.cs Models\IdentityModel.cs Account\Manage.aspx.cs - Update connection string to use the MySQL database as needed 在 Web.config 中修改 connection string, 將原為 改為 以上資料庫位置、名稱、使用者帳號、密碼請改為你的測試環境設定值. - In the IdentityModel.cs, let ApplicationUser class extend from Identity user in LibSQL AspNet.Identity.MySQL 將 專案建立的 IdentityModel.cs 中 ApplicationUser 類別原繼承的 IdentityUser (Microsoft.AspNet.Identity.EntityFramework), 改為繼承我們的 LibSQL.DIdentity.User. - 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)); 將 App_Start\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 由於 專案自動建立的 IdentityModels.cs ApplicationUser 物件被改為繼承自 我們自己的 LibSQL.DIdentity.User 類別. 因此 如果需要變更 ApplicationUser類別屬性時, 就要到我們的專案中實作方法 Insert(), GetUserByName(), GetUserById() and Update()等內容, 變更對應的SQL指令.