zoukankan      html  css  js  c++  java
  • .net core 2.2 修改IdentityUser主键标识类型

    .net core2.2,生成WebApi或者MVC项目后,Identity

    1.增加ApplicationUser.cs文件,内容如下

       public class ApplicationUser : IdentityUser<int>
        {
    
        }
    
        public class ApplicationRole : IdentityRole<int>
        {
    
        }

    2.修改ApplicationDbContext.cs文件,内容如下

        public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
        {
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
                : base(options)
            {
    } }

    3.修改Startup.cs,内容如下

            public void ConfigureServices(IServiceCollection services)
            {
                services.Configure<CookiePolicyOptions>(options =>
                {
                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                    options.CheckConsentNeeded = context => true;
                    options.MinimumSameSitePolicy = SameSiteMode.None;
                });
    
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(
                        Configuration.GetConnectionString("DriverBase")));
                services.AddDefaultIdentity<ApplicationUser>()
                    .AddDefaultUI(UIFramework.Bootstrap4)
                    .AddEntityFrameworkStores<ApplicationDbContext>();
    
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            }

     4.在PM运行Add-Migration CreateIdentitySchema

    PM> Add-Migration CreateIdentitySchema
      Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.2.1-servicing-10028 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
      To undo this action, use Remove-Migration.

    5.在PM运行Update-Database

    PM> Update-Database

    6.打开ViewsShared\_LoginPartial.cshtml文件,找到如下内容

    @using Microsoft.AspNetCore.Identity
    @inject SignInManager<IdentityUser> SignInManager
    @inject UserManager<IdentityUser> UserManager

    修改为:

    @using Microsoft.AspNetCore.Identity
    @inject SignInManager<ApplicationUser> SignInManager
    @inject UserManager<ApplicationUser> UserManager
  • 相关阅读:
    盘一盘 synchronized (二)—— 偏向锁批量重偏向与批量撤销
    盘一盘 synchronized (一)—— 从打印Java对象头说起
    盘一盘 Thread源码
    盘一盘 System.out.println()
    Java中的单例模式
    Spring AOP SpringBoot集成
    第16周作业
    第15周作业
    迟到的第14周作业
    第13周作业集
  • 原文地址:https://www.cnblogs.com/baddot/p/10419303.html
Copyright © 2011-2022 走看看