亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

Asp.NET Core Identity 是.Net自帶的身份認(rèn)證系統(tǒng),支持用戶界面 (UI) 登錄功能,并且管理用戶、密碼、配置文件數(shù)據(jù)、角色、聲明、令牌、電子郵件確認(rèn)等等。使用Visual Studio創(chuàng)建帶有identity的項目時,使用SqlServer作為缺省的數(shù)據(jù)庫,本文介紹如何改造為多種數(shù)據(jù)庫支持。

首先,使用Visual Studio 2022創(chuàng)建一個新的Asp.Net Core Web項目,名稱為TestIdentity,選擇身份認(rèn)證類型為個人賬戶:

Asp.Net Core Identity 多數(shù)據(jù)庫支持

 


創(chuàng)建的項目結(jié)構(gòu)如下:

Asp.Net Core Identity 多數(shù)據(jù)庫支持

 


在Data目錄下保存的是身份認(rèn)證的DbContext,名稱為ApplicationDbContext,還有基于SqlServer的遷移文件。我們所要做的第一件事情是將SqlServer部分移動到另一個項目中,然后再增加對其它數(shù)據(jù)庫類型的支持。

現(xiàn)在我們在解決方案中創(chuàng)建一個新的類庫項目,名稱為IdentityEF,在這個項目中安裝包
Microsoft.AspNetCore.Identity.EntityFrameworkCore。然后將ApplicationDbContext移動到這個項目中。

Asp.Net Core Identity 多數(shù)據(jù)庫支持

 


然后我們再創(chuàng)建另一個類庫項目,負(fù)責(zé)SqlServer數(shù)據(jù)庫的遷移,名稱為IdentityEF.SqlServer,在這個項目中安裝包
Microsoft.EntityFrameworkCore.SqlServer和Microsoft.EntityFrameworkCore.Tools,還要增加對IdentityEF的項目引用,然后將TestIdentity中Data目錄下的Migrations子目錄移動到這個項目中:

Asp.Net Core Identity 多數(shù)據(jù)庫支持

 

然后在這個項目中增加新的類DbContextFactory,代碼如下:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestIdentity.Data;

namespace IdentityEF.SqlServer
{
    public class DbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            optionsBuilder.UseSqlServer("Server=(localdb)\mssqllocaldb;Database=aspnet-TestIdentity-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true",
              x => x.MigrationsAssembly("IdentityEF.SqlServer"));
            return new ApplicationDbContext(optionsBuilder.Options);
        }
    }
}

請注意,上面的數(shù)據(jù)庫名稱與TestIdentity項目中appsettings.json中定義的DefaultConnection是一樣的,這樣,生成的數(shù)據(jù)庫在TestIdentity中可以直接使用。

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=aspnet-TestIdentity-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

再增加一個依賴注入擴(kuò)展IdentityEFExtension,方便在Web應(yīng)用中的引用:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestIdentity.Data;

namespace IdentityEF.SqlServer
{
    public static class IdentityEFExtension
    {
        public static IServiceCollection AddIdentityEFSqlServer(this IServiceCollection services, IConfiguration Configuration)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                x => x.MigrationsAssembly("IdentityEF.SqlServer")));
            return services;
        }
    }
}

到這里,改造基本完畢,在Web應(yīng)TestIdentity項目中,增加對這兩個項目的引用,然后改造Program.cs,將原有的部分注釋掉,增加AddIdentityEFSqlServer:

//// Add services to the container.
//var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
//builder.Services.AddDbContext<ApplicationDbContext>(options =>
//    options.UseSqlServer(connectionString));
//builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddIdentityEFSqlServer(builder.Configuration);

現(xiàn)在,可以在包管理器中,使用Update-Database創(chuàng)建數(shù)據(jù)庫。首先,將IdentityEF.SqlServer項目設(shè)置為啟動項目,在包管理器中,將缺省項目也設(shè)置為IdentityEF.SqlServer:

Asp.Net Core Identity 多數(shù)據(jù)庫支持

 


然后運行Update-Database,順利的化,數(shù)據(jù)庫就生成了。將啟動項目改回到TestIdentity,運行項目,我們可以注冊用戶并進(jìn)行登錄了。到這里,針對SqlServer的部分已經(jīng)從Web項目中分離,現(xiàn)在,我們增加對其它數(shù)據(jù)庫類型的支持,比如,我們增加Sqlite的支持。

創(chuàng)建一個新的類庫,名稱為IdentityEF.Sqlite,增加程序包
Microsoft.EntityFrameworkCore.Sqlite和Microsoft.EntityFrameworkCore.Tools,還要增加對IdentityEF的項目引用,然后增加DbContextFactory:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using TestIdentityEF.Data;

namespace IdentityEF.Sqlite
{
    public class DbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            optionsBuilder.UseSqlite("DataSource=mydatabase.db;",
                x => x.MigrationsAssembly("IdentityEF.Sqlite"));

            return new ApplicationDbContext(optionsBuilder.Options);
        }
    }
}

還增加依賴注入擴(kuò)展:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestIdentity.Data;

namespace IdentityEF.Sqlite
{
    public static class IdentityEFExtension
    {
        public static IServiceCollection AddIdentityEFSqlite(this IServiceCollection services, IConfiguration Configuration)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlite(Configuration.GetConnectionString("IdentityConnection"),
                x => x.MigrationsAssembly("IdentityEF.Sqlite")));
            return services;
        }
    }
}

項目的結(jié)構(gòu)如下:

Asp.Net Core Identity 多數(shù)據(jù)庫支持

 


現(xiàn)在,我們需要生成遷移文件和數(shù)據(jù)庫。將項目IdentityEF.Sqlite設(shè)置為啟動項目,在程序包管理器中,將IdentityEF.Sqlite設(shè)置為缺省項目:

Asp.Net Core Identity 多數(shù)據(jù)庫支持

 


在程序包管理器中運行:

Add-Migration init

如果一切順利,在項目文件中會增加遷移文件:

Asp.Net Core Identity 多數(shù)據(jù)庫支持

 


然后運行Update-Database,我們會發(fā)現(xiàn),項目中多了db文件:

Asp.Net Core Identity 多數(shù)據(jù)庫支持

 


最后,改造一下Web應(yīng)用,使其支持Sqlite數(shù)據(jù)庫,并且可以通過配置文件進(jìn)行切換。在項目中增加對IdentityEF.Sqlite的引用,然后修改Program.cs:

if (builder.Configuration["DbType"]=="SqlServer")
    builder.Services.AddIdentityEFSqlServer(builder.Configuration);
else
    builder.Services.AddIdentityEFSqlite(builder.Configuration);

在配置文件中使用DbType切換數(shù)據(jù)庫的類型:

{
  "ConnectionStrings": {
    //"DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=aspnet-TestIdentity-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true",
    "DefaultConnection": "DataSource=D:\Asp.Net Core\TestIdentityEF\IdentityEF.Sqlite\mydatabase.db"
  },
  "DbType": "Sqlite",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

完整的項目代碼可以從github下載:
https://github.com/zhenl/TestIdentityEF 。

文章來自
https://www.cnblogs.com/zhenl/p/16340890.html

分享到:
標(biāo)簽:Net
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達(dá)人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定