From: 011netservice@gmail.com
Date: 2020-04-22
Subject: Custom storage providers for ASP.NET Core Identity .NET 6.0

本文示範如何客製化 Visual Studio 2022 ASP.NET Core Web Application MVC Individual Account .NET 6.0 的網站樣本, 取消使用 Microsoft.AspNetCore.Identity.EntityFrameworkCore, 並將原本連接 SQL Server Express LocalDB 資料庫, 改為連接到 MySql 資料庫.

舊版修改 Visual Studio 2019, NET Framework 4.8 的環境, 請參考這裡 .

歡迎來信交流, 訂購軟體需求.

步驟:
  1. 建立網站
  2. 解除安裝 Microsoft.AspNet.Identity.EntityFramework 元件 及 EntityFrameWork 元件
  3. 移除參考 EntityFramework 及 EntityFramework.SQLServer
  4. 加入自訂的 ASP.NET Identity 實作程式
  5. 將 Microsoft.AspNet.Identity.EntityFramework 改為使用 ZIdentitySqlServer
  6. 將 IdentityModel.cs 之 ApplicationUser 改為繼承 ZUser, ApplicationDbContext 改為繼承 ZSqlClient
  7. 將 IdntityConfig.cs 取得的 ApplicationDBContext 改為 ZSqlClient
  8. 將 Web.Config 之 ConnnectionString 改到新的資料庫位置
  9. 建立新資料庫, 移除舊資料庫
  10. 編譯執行測試
步驟詳細說明:
  1. 建立網站
    開啟 Visual Studio 2022, 選擇 Create a new project, ASP.NET Core Web App, Project name="NET60MvcIAMySql", Framework=".NET 6.0 (Long Term Support)", Authentication Type="Individual Accounts", Configure for HTTP=Checked 建立網站, 完成後如下:

    Areas/Identity/Pages中只剩下 _ViewStart.cshtml, 其餘的 Razor Page 在 ASP.NET Core 2.1 都被收藏在 Microsoft.AspNetCore.Identity.UI NuGet 中, 必須取出來才能修改.
    ASPNETCoreWebAppMvcIAScaffolder1 https://blog.darkthread.net/blog/aspnetcore-identity/ □ Razor Page 為主 □ LocalDB 資料庫檔案在 %userprofile%\asp-* □ Areas/Identity/Pages中只剩下 _ViewStart.cshtml, 其餘的 Razor Page(註冊, 登入, 修改密碼, 忘記密碼...), 在 ASP.NET Core 2.1之後, 都被收藏在 Microsoft.AspNetCore.Identity.UI NuGet 中, 必須取出來才能修改. .NET 6.0 取出方法: Scaffold Identity into an MVC project with authorization ● From Solution Explorer, right-click on the project > Add > New Scaffolded Item. ● From the left pane of the Add New Scaffolded Item dialog, select Identity. Select Identity in the center pane. Select the Add button. ● In the Add Identity dialog, select the options you want. ○ Select your existing layout page so your layout file isn't overwritten with incorrect markup. When an existing _Layout.cshtml file is selected, it is not overwritten. For example: ○ ~/Pages/Shared/_Layout.cshtml for Razor Pages or Blazor Server projects with existing Razor Pages infrastructure. ○ ~/Views/Shared/_Layout.cshtml for MVC projects or Blazor Server projects with existing MVC infrastructure. ● To use your existing data context, select at least one file to override. You must select at least one file to add your data context. ○ Select your data context class. ○ Select Add. ● To create a new user context and possibly create a custom user class for Identity: ○ Select the + button to create a new Data context class. Accept the default value or specify a class (for example, MyApplication.Data.ApplicationDbContext). ○ Select Add. Note: If you're creating a new user context, you don't have to select a file to override. Scaffold Identity in ASP.NET Core projects: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-6.0&tabs=visual-studio https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-6.0&tabs=visual-studio https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-6.0&tabs=visual-studio#scaffold-identity-into-a-razor-project-with-authorization Introduction to Identity on ASP.NET Core: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-6.0&tabs=visual-studio Sample code: https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/security/authentication/identity/sample (舊版取出方法可參考這篇: https://devblogs.microsoft.com/dotnet/aspnetcore-2-1-identity-ui) ???? ????? 參考這篇 這篇

  2. 移除元件:
    Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore 6.0.16
    Microsoft.EntityFrameworkCore.SqlServer 6.0.16
    Microsoft.EntityFrameworkCore.Tools 6.0.16
    Microsoft.AspNetCore.Identity.EntityFrameworkCore 6.0.16
    徹底刪除 EntityFrameworkCore 元件


  3. 將 \Data\Migrations 從專案中 Exclude from project:
    其中這2個檔案可參考保存為文件, 作為(以 cs 建立 Identity schema 和 Snapshot)的範例程式碼.
    ApplicationDbContextModelSnapshot.cs
    00000000000000_CreateIdentitySchema.cs
    3010

  4. 加入自訂的 ASP.NET Identity 實作程式
    本文提供 Visual Studio 2019 的程式庫版本專案補齊:
    1. ZLib: 公用程式庫.
    2. ZSqlClient: 存取 SQL Server 資料庫公用程式庫. (也可用於 MSSQLLocalDB 或 SQL Express 資料庫)
    3. ZIdentitySqlServer: 替代 Microsoft.AspNet.Identity.EntityFramework, 存取ASP.NET Identity身分認證資料庫的公用程式庫.
    請將這三個專案, 加入 Solution 中並參考引用.


    ASP.NET Identity身分認證資料庫 ERD of ASP.NET Identity
  5. 將 Microsoft.AspNet.Identity.EntityFramework 改為使用 ZIdentitySqlServer
    將專案中, 所有的 using Microsoft.AspNet.Identity.EntityFramework; 的 .cs 程式, 都改成 using ZIdentitySqlServer;
    需要修改的程式清單如下:
    App_Start\IdentityConfig.cs
    App_Start\Startup.Auth.cs
    Models\IdentityModel.cs
    Account\Manage.aspx.cs

  6. 將 IdentityModel.cs 之 ApplicationUser 改為繼承 ZUser, ApplicationDbContext 改為繼承 ZSqlClient


  7. 將 IdntityConfig.cs 取得的 ApplicationDBContext 改為 ZSqlClient


  8. 將 Web.Config 之 ConnnectionString 改到新的資料庫位置


  9. 建立新資料庫, 移除舊資料庫
    參考 CreateDBAspNet1-SqlServer.sql 建立 SQL Server 新資料庫 DBAspNet1
    移除在 App_Data 目錄下的舊資料庫 MSSQLLocalDB:
    App_Data\*.MDF
    App_Data\*.LDF

  10. 編譯執行測試
    - 測試帳號:
    - test1@some.com, aaBB11!!
    - test2@some.com, aaBB22!!


reference

ref:
先看這篇, 解除使用 Microsoft.AspNetCore.EntityFramework.Identity 改到 新改出來的 storage provider Custom storage providers for ASP.NET Core Identity
var builder = WebApplication.CreateBuilder(args); // Add identity types builder.Services.AddIdentity() .AddDefaultTokenProviders(); // Identity Services builder.Services.AddTransient, CustomUserStore>(); builder.Services.AddTransient, CustomRoleStore>(); var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); builder.Services.AddTransient(e => new SqlConnection(connectionString)); builder.Services.AddTransient(); // additional configuration builder.Services.AddRazorPages(); var app = builder.Build(); 再將 Identity scaffolder 釋放出來, 解除使用 ASP.NET Core 2.1.0-preview1: Introducing Identity UI as a library
IdentitySample
Entity Framework 6
Entity Framework Core
Implementing a Custom MySQL ASP.NET Identity Storage Provider
Overview of Custom Storage Providers for ASP.NET Identity
Creating and Configuring a Model
DbContext Lifetime, Configuration, and Initialization
Custom storage providers for ASP.NET Core Identity
Identity model customization in ASP.NET Core
CustomIdentityProviderSample

Log

Log:
20230422, Honda, Create.

開放時間 09:00 ~ 18:00 |  🛈 |   |   |