虽然Asp.Net Core.Identity提供了IdentityUser类,但是在有些情况下我们需要一些额外的用户信息,比如性别,年龄等,这时候就需要来扩展IdentityUser类以达到我们的需求。

namespace Microsoft.AspNetCore.Identity
{
//
// 摘要:
// Represents a user in the identity system
//
// 类型参数:
// TKey:
// The type used for the primary key for the user.
public class IdentityUser<TKey> where TKey : IEquatable<TKey>
{
//
// 摘要:
// Initializes a new instance of Microsoft.AspNetCore.Identity.IdentityUser`1.
public IdentityUser();
//
// 摘要:
// Initializes a new instance of Microsoft.AspNetCore.Identity.IdentityUser`1.
//
// 参数:
// userName:
// The user name.
public IdentityUser(string userName); //
// 摘要:
// Gets or sets the date and time, in UTC, when any user lockout ends.
//
// 言论:
// A value in the past means the user is not locked out.
public virtual DateTimeOffset? LockoutEnd { get; set; }
//
// 摘要:
// Gets or sets a flag indicating if two factor authentication is enabled for this
// user.
[PersonalData]
public virtual bool TwoFactorEnabled { get; set; }
//
// 摘要:
// Gets or sets a flag indicating if a user has confirmed their telephone address.
[PersonalData]
public virtual bool PhoneNumberConfirmed { get; set; }
//
// 摘要:
// Gets or sets a telephone number for the user.
[ProtectedPersonalData]
public virtual string PhoneNumber { get; set; }
//
// 摘要:
// A random value that must change whenever a user is persisted to the store
public virtual string ConcurrencyStamp { get; set; }
//
// 摘要:
// A random value that must change whenever a users credentials change (password
// changed, login removed)
public virtual string SecurityStamp { get; set; }
//
// 摘要:
// Gets or sets a salted and hashed representation of the password for this user.
public virtual string PasswordHash { get; set; }
//
// 摘要:
// Gets or sets a flag indicating if a user has confirmed their email address.
[PersonalData]
public virtual bool EmailConfirmed { get; set; }
//
// 摘要:
// Gets or sets the normalized email address for this user.
public virtual string NormalizedEmail { get; set; }
//
// 摘要:
// Gets or sets the email address for this user.
[ProtectedPersonalData]
public virtual string Email { get; set; }
//
// 摘要:
// Gets or sets the normalized user name for this user.
public virtual string NormalizedUserName { get; set; }
//
// 摘要:
// Gets or sets the user name for this user.
[ProtectedPersonalData]
public virtual string UserName { get; set; }
//
// 摘要:
// Gets or sets the primary key for this user.
[PersonalData]
public virtual TKey Id { get; set; }
//
// 摘要:
// Gets or sets a flag indicating if the user could be locked out.
public virtual bool LockoutEnabled { get; set; }
//
// 摘要:
// Gets or sets the number of failed login attempts for the current user.
public virtual int AccessFailedCount { get; set; } //
// 摘要:
// Returns the username for this user.
public override string ToString();
}
}

  接下来我们就来定义一个继承自IdentityUser类的ApplicationUser类。

public class ApplicationUser : IdentityUser
{
public string City { get; set; }
}

  然后在AppDbContext中继承泛型 IdentityDbContext<ApplicationUser>类。并且在Startup中注入的时候把IdentityUser修改为ApplicationUser后,再做数据库迁移,如果之前已经迁移过,则需要替换项目中所有的IdentityUser为ApplicationUser。

public class AppDbContext:IdentityDbContext<ApplicationUser>
{ public AppDbContext(DbContextOptions<AppDbContext> options):base(options)
{
} public DbSet<Student> Students { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Seed();
}
}
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddErrorDescriber<CustomIdentityErrorDescriber>()
.AddEntityFrameworkStores<AppDbContext>();

扩展Asp.Net Core中的IdentityUser类的更多相关文章

  1. ASP.NET Core中的Startup类

    ASP.NET Core程序要求有一个启动类.按照惯例,启动类的名字是 "Startup" .Startup类负责配置请求管道,处理应用程序的所有请求.你可以指定在Main方法中使 ...

  2. 在ASP.Net Core 中使用枚举类而不是枚举

    前言:我相信大家在编写代码时经常会遇到各种状态值,而且为了避免硬编码和代码中出现魔法数,通常我们都会定义一个枚举,来表示各种状态值,直到我看到Java中这样使用枚举,我再想C# 中可不可以这样写,今天 ...

  3. ASP.NET Core 中文文档 第三章 原理(1)应用程序启动

    原文:Application Startup 作者:Steve Smith 翻译:刘怡(AlexLEWIS) 校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程 ...

  4. ASP.NET Core中,UseDeveloperExceptionPage扩展方法会吃掉异常

    在ASP.NET Core中Startup类的Configure方法中,有一个扩展方法叫UseDeveloperExceptionPage,如下所示: // This method gets call ...

  5. ASP.NET Core中的Startup

    原文:链接 Startup.cs的作用: 配置各服务和HTTP请求管道. Startup类: ASP.NET Core中使用按惯例Startup命名的类Startup.cs: (可选)包括Config ...

  6. (1)ASP.NET Core 应用启动Startup类简介

    1.前言 Core与早期版本的 ASP.NET 对比,配置应用程序的方式的 Global.asax.FilterConfig.cs和RouteConfig.cs 都被Program.cs 和 Star ...

  7. 在ASP.NET Core中使用百度在线编辑器UEditor

    在ASP.NET Core中使用百度在线编辑器UEditor 0x00 起因 最近需要一个在线编辑器,之前听人说过百度的UEditor不错,去官网下了一个.不过服务端只有ASP.NET版的,如果是为了 ...

  8. ASP.NET Core中的依赖注入(1):控制反转(IoC)

    ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制,ASP.NET通过定义接口的方式对它们进行了"标准化&qu ...

  9. ASP.NET Core中的依赖注入(2):依赖注入(DI)

    IoC主要体现了这样一种设计思想:通过将一组通用流程的控制从应用转移到框架之中以实现对流程的复用,同时采用"好莱坞原则"是应用程序以被动的方式实现对流程的定制.我们可以采用若干设计 ...

随机推荐

  1. 坑爹的IE8

    1.不能用trim(),要用$.trim()    var aa = $("#id").val().trim()  这样素不行的,要变成这样Jquery的方式 var aa = $ ...

  2. web+页面支持批量下载吗

    一.此方法火狐有些版本是不支持的 window.location.href = 'https://*****.oss-cn-**.aliyuncs.com/*********';二.为了解决火狐有些版 ...

  3. Django系列(四):多表操作

    1.创建模型 例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对一(on ...

  4. spark常见错误【持续更新】

    错误1.错误: 找不到或无法加载主类 idea.scala代码 idea 导入的scala工程,编写代码后报该错误. 原因:\src\main\scala 包路径下没有将scala这个包设置成Sour ...

  5. 3、Web server 之httpd2.2 配置说明

    http协议实现的程序 静态(httpd, nginx, lighttpd) 动态 (IIS, tomcat,  jetty,  jboss,  resin,  websphere, weblogic ...

  6. Hadoop mapreduce过程分析

    原理图: 中间结果的排序与溢出(spill)流程图 map分析: (1).输入分片(input split):在进行mapreduce之前,mapreduce首先会对输入文件进行输入分片(input ...

  7. mysql触发器个人实战

    create trigger idtriggerbefore insert on flow_management_copy1for each ROWBEGIN SET new.ID= CONCAT(R ...

  8. ICEM-圆柱与长方体相切

    原视频下载地址:https://yunpan.cn/cqvgLe39ZU4Ke  访问密码 c1c9

  9. rethinkDB python入门

    Start the server For a more detailed look, make sure to read the quickstart. $ rethinkdb Import the ...

  10. LeetCode 第 149 场周赛

    成绩 一.一年中的第几天(LeetCode-1154) 1.1 题目描述 1.2 解题思路 比较容易的一题,搞清楚平年.闰年的判定规则,就很容易做出来. 1.3 解题代码 class Solution ...