在ASP.NET Web 應(yīng)用下基于 MySQL 使用 Identity 2

1.創(chuàng)建 ASP.NET Web APP

2.替換默認(rèn)的數(shù)據(jù)庫鏈接

Web.config 文件的 connectionStrings 配置節(jié)中替換默認(rèn)的鏈接字符串為:
<add name="DefaultConnection" connectionString="Server=localhost;Port=13306;Database=databasename;User id=uid;Password=somepass;" providerName="MySql.Data.MySqlClient" />

3.創(chuàng)建默認(rèn)數(shù)據(jù)表

創(chuàng)建表的SQL腳本

更改數(shù)據(jù)表區(qū)分大小寫
在[mysqld]節(jié)點(diǎn)下,加入一行: lower_case_table_names=1

4.刪除默認(rèn)的 Microsoft.AspNet.Identity.EntityFramework 包

使用命令行: Uninstall-Package Microsoft.AspNet.Identity.EntityFramework

直接使用安裝界面點(diǎn)擊刪除

如有依賴包需要先刪除,如 Microsoft.AspNet.Identity.EntityFramework.zh-Hans

5.安裝 MySql.AspNet.Identity 包

命令行: Install-Package MySql.AspNet.Identity -Version 2.1.1

Dependencies

6.替換代碼

1.替換引用包

~/Models/IdentityModels.cs (以及其他引用到這兩個(gè)包的)文件中刪除以下兩個(gè)引用

using System.Data.Entity;
using Microsoft.AspNet.Identity.EntityFramework;

替換為

using MySql.AspNet.Identity;

2.刪除不必要代碼

~/Models/IdentityModels.cs 中刪除這個(gè)類

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
  public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
  {
  }
  public static ApplicationDbContext Create()
  {
    return new ApplicationDbContext();
  }
}

~/App_Start/Startup.Auth.cs 中刪除這行代碼

app.CreatePerOwinContext(ApplicationDbContext.Create);

3.刪除引用替換代碼

~/App_Start/IdentityConfig.cs 文件中刪除以下引用

using System.Data.Entity;
using Microsoft.AspNet.Identity.EntityFramework;

添加以下引用

using MySql.AspNet.Identity;

替換代碼

// 替換掉被注釋的這行代碼
// var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
var manager = new ApplicationUserManager(new MySqlUserStore<ApplicationUser>());

7.運(yùn)行后功能可正常使用

項(xiàng)目代碼見GitHub - Identity2MySql

參考

  1. ASP.NET Identity 2.1 implementation for MySQL
  2. Overview of Custom Storage Providers for ASP.NET Identity
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容