虽然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. js的新生代垃圾回收

    推荐阅读:https://www.cnblogs.com/chengxs/p/10919311.html 在进行老生代的标记清除法回收以前,还会有一个新生代的垃圾回收算法执行. 新生代和老生代 所谓新 ...

  2. js的老生代垃圾回收

    推荐阅读:<JS 闯关记>之垃圾回收和内存管理 常见的垃圾回收有2种策略:标记清除 和 引用计数 标记清除 会遍历堆中所有的对象,然后标记活的对象,在标记完成后,销毁所有没有被标记的对象. ...

  3. Java基础系列 - HashMap

    package com.test3; import java.util.ArrayList; import java.util.HashMap; import java.util.List; impo ...

  4. docker 搭建registry

    Docke官方提供了Docker Hub网站来作为一个公开的集中仓库.然而,本地访问Docker Hub速度往往很慢,并且很多时候我们需要一个本地的私有仓库只供网内使用.Docker仓库实际上提供两方 ...

  5. 浅谈无旋treap(fhq_treap)

    一.简介 无旋Treap(fhq_treap),是一种不用旋转的treap,其代码复杂度不高,应用范围广(能代替普通treap和splay的所有功能),是一种极其强大的平衡树. 无旋Treap是一个叫 ...

  6. POI2010 Bridges

    好题\(Q\omega Q\) 我们考虑这个东西要求最大值最小,显然一眼二分答案对吧. 问题在于如何\(check\),我们二分答案之后把问题转换成了混合图如何求欧拉回路. 考虑欧拉回路的性质,每一个 ...

  7. xposed代码示例

    package com.example.xposedhook; import android.util.Log; import de.robv.android.xposed.IXposedHookLo ...

  8. Cesium入门-2-增加地形

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. angular 中的dom操作(原生JS)

    <h2>这是一个home组件--DOM操作演示</h2> <div id="box"> this is box </div> < ...

  10. Strin类

    常见构造方法 • public String():空构造 • public String(byte[] bytes):把字节数组转成字符串 • public String(byte[] bytes,i ...