更改.net core 3中默认的身份认证生成的数据库名称。

1.代替默认的名称IdentityUser

    /// <summary>
    /// Web用户。
    /// </summary>
    public class WebUser : IdentityUser<Guid>
    {
        /// <summary>
        /// 补充昵称。
        /// </summary>
        public string NickName { get; set; }
    }

2.重写上下文类即可

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

        /// <summary>
        /// 重写。
        /// </summary>
        /// <param name="builder"></param>
        protected override void OnModelCreating(ModelBuilder builder)
        {
            var maxKeyLength = 256;
            builder.Entity<WebUser>(b =>
            {
                // Primary key
                b.HasKey(u => u.Id);

                // Indexes for "normalized" username and email, to allow efficient lookups
                b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique();
                b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex");

                // Maps to the AspNetUsers table
                b.ToTable(nameof(WebUser));

                // A concurrency token for use with the optimistic concurrency checking
                b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken();

                // Limit the size of columns to use efficient database types
                b.Property(u => u.UserName).HasMaxLength(256);
                b.Property(u => u.NormalizedUserName).HasMaxLength(256);
                b.Property(u => u.Email).HasMaxLength(256);
                b.Property(u => u.NormalizedEmail).HasMaxLength(256);

                // The relationships between User and other entity types
                // Note that these relationships are configured with no navigation properties

                // Each User can have many UserClaims
                b.HasMany<IdentityUserClaim<Guid>>().WithOne().HasForeignKey(uc => uc.UserId).IsRequired();

                // Each User can have many UserLogins
                b.HasMany<IdentityUserLogin<Guid>>().WithOne().HasForeignKey(ul => ul.UserId).IsRequired();

                // Each User can have many UserTokens
                b.HasMany<IdentityUserToken<Guid>>().WithOne().HasForeignKey(ut => ut.UserId).IsRequired();

                // Each User can have many entries in the UserRole join table
                b.HasMany<IdentityUserRole<Guid>>().WithOne().HasForeignKey(ur => ur.UserId).IsRequired();
            });

            builder.Entity<IdentityUserClaim<Guid>>(b =>
            {
                // Primary key
                b.HasKey(uc => uc.Id);

                // Maps to the AspNetUserClaims table
                b.ToTable("AspNetUserClaims");
            });

            builder.Entity<IdentityUserLogin<Guid>>(b =>
            {
                // Composite primary key consisting of the LoginProvider and the key to use
                // with that provider
                b.HasKey(l => new { l.LoginProvider, l.ProviderKey });

                // Limit the size of the composite key columns due to common DB restrictions
                b.Property(l => l.LoginProvider).HasMaxLength(128);
                b.Property(l => l.ProviderKey).HasMaxLength(128);

                // Maps to the AspNetUserLogins table
                b.ToTable("AspNetUserLogins");
            });

            builder.Entity<IdentityUserToken<Guid>>(b =>
            {
                // Composite primary key consisting of the UserId, LoginProvider and Name
                b.HasKey(t => new { t.UserId, t.LoginProvider, t.Name });

                // Limit the size of the composite key columns due to common DB restrictions
                b.Property(t => t.LoginProvider).HasMaxLength(maxKeyLength);
                b.Property(t => t.Name).HasMaxLength(maxKeyLength);

                // Maps to the AspNetUserTokens table
                b.ToTable("AspNetUserTokens");
            });

            builder.Entity<ApplicationRole>(b =>
            {
                // Primary key
                b.HasKey(r => r.Id);

                // Index for "normalized" role name to allow efficient lookups
                b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique();

                // Maps to the AspNetRoles table
                b.ToTable("AspNetRoles");

                // A concurrency token for use with the optimistic concurrency checking
                b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken();

                // Limit the size of columns to use efficient database types
                b.Property(u => u.Name).HasMaxLength(256);
                b.Property(u => u.NormalizedName).HasMaxLength(256);

                // The relationships between Role and other entity types
                // Note that these relationships are configured with no navigation properties

                // Each Role can have many entries in the UserRole join table
                b.HasMany<IdentityUserRole<Guid>>().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired();

                // Each Role can have many associated RoleClaims
                b.HasMany<IdentityRoleClaim<Guid>>().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
            });

            builder.Entity<IdentityRoleClaim<Guid>>(b =>
            {
                // Primary key
                b.HasKey(rc => rc.Id);

                // Maps to the AspNetRoleClaims table
                b.ToTable("AspNetRoleClaims");
            });

            builder.Entity<IdentityUserRole<Guid>>(b =>
            {
                // Primary key
                b.HasKey(r => new { r.UserId, r.RoleId });

                // Maps to the AspNetUserRoles table
                b.ToTable("AspNetUserRoles");
            });
        }
    }

其实官方文档都有写。

原文地址:https://www.cnblogs.com/yeqifeng2288/p/11444210.html

时间: 2024-08-29 23:43:33

更改.net core 3中默认的身份认证生成的数据库名称。的相关文章

ASP.NET Core实现 随处可见的基本身份认证

原文:ASP.NET Core实现 随处可见的基本身份认证 概览 在HTTP中,基本认证(Basic access authentication,简称BA认证)是一种用来允许网页浏览器或其他客户端程序在请求资源时提供用户名和口令形式的身份凭证的一种登录验证方式,不要求cookie,session identifier.login page等标记或载体. 优点:基本上所有流行的网页浏览器都支持BA认证. 缺点:明文传输密钥和口令(容易被拦截); 没有对服务器返回的信息提供保护. https://e

PHP中对用户身份认证实现两种方法

用户在设计和维护站点的时候,经常需要限制对某些重要文件或信息的访问.通常,我们可以采用内置于WEB服务器的基于HTTP协议的用户身份验证机制. 当访问者浏览受保护页面时,客户端浏览器会弹出对话窗口要求用户输入用户名和密码,对用户的身份进行验证,以决定用户是否有权访问页面.下面用两种方法来说明其实现原理.  一.用HTTP标头来实现  标头是服务器以HTTP协议传送HTML信息到浏览器前所送出的字串.HTTP采用一种挑战/响应模式对试图进入受密码保护区域的用户进行身份验证.具体来说,当用户首次向W

SQL Server 数据库身份认证以及包含数据库

首先分为SQL Server 认证与Windows 身份认证. SQL Server 认证可以运行以下语句来查询 1 select * from sys.sql_logins 管理员可以直接修改密码,但无法知晓原有密码原文,SQL Server使用混淆算法来保护安全性不如Windows 身份认证, Windows认证模式 首先分为本机账号与域账号 SQL Server 将认证和授权分散给了不同的对象来完成,SQL Server 的“登入名”(Login)用于认证,连接SQL Server 的SQ

ASP.NET MVC 中实现真实世界中的 OAuth 身份认证

http://www.oschina.net/translate/real-world-oauth-using-aspnet-mvc?cmp

Spring Security 之身份认证

Spring Security可以运行在不同的身份认证环境中,当我们推荐用户使用Spring Security进行身份认证但并不推荐集成到容器管理的身份认证中时,但当你集成到自己的身份认证系统时,它依然是支持的. 1. Spring Security中的身份认证是什么? 现在让我们考虑一下每个人都熟悉的标准身份认证场景: (1)用户打算使用用户名和密码登陆系统 (2)系统验证用户名和密码合法 (3)得到用户信息的上下文(角色等信息) (4)为用户建立一个安全上下文 (5)用户接下来可能执行一些权

说道说道SpringSecurity身份认证

Spring Security可以运行在不同的身份认证环境中,当我们推荐用户使用Spring Security进行身份认证但并不推荐集成到容器管理的身份认证中时,但当你集成到自己的身份认证系统时,它依然是支持的. 1. Spring Security中的身份认证是什么? 现在让我们考虑一下每个人都熟悉的标准身份认证场景: (1)用户打算使用用户名和密码登陆系统 (2)系统验证用户名和密码合法 (3)得到用户信息的上下文(角色等信息) (4)为用户建立一个安全上下文 (5)用户接下来可能执行一些权

Spring Security 实现身份认证

Spring Security可以运行在不同的身份认证环境中,当我们推荐用户使用Spring Security进行身份认证但并不推荐集成到容器管理的身份认证中时,但当你集成到自己的身份认证系统时,它依然是支持的. 1. Spring Security中的身份认证是什么? 现在让我们考虑一下每个人都熟悉的标准身份认证场景: (1)用户打算使用用户名和密码登陆系统 (2)系统验证用户名和密码合法 (3)得到用户信息的上下文(角色等信息) (4)为用户建立一个安全上下文 (5)用户接下来可能执行一些权

.NetCore采取JWT方式进行身份认证

验证与授权 Authentication(身份认证) 认证是系统对请求的用户进行身份识别的过程. Authorization (授权) 授权是对认证通过后的用户进行权限分配的过程.授权简单理解就是:识别认证后用户所拥有哪些权限,从而开放服务器相对应的资源: 我们通俗点来解释身份验证与授权:验证确认用户是否允许访问,授权给予登录后的用户指定权限标识(通过这个标识,服务端可允许用户进行访问和操作): 在进行讲解Jwt身份认证前我们来回一下过去我们在MVC.WebForm这些站点都是怎样进行身份认证的

IE11下Forms身份认证无法保存Cookie的问题

ASP.NET中使用Forms身份认证常见的做法如下: 1. 网站根目录下的Web.config添加authentication节点 <authentication mode="Forms"> <forms name="MyAuth" loginUrl="manager/Login.aspx" defaultUrl="manager/default.aspx" protection="All&quo