扩展Asp.Net Core中的IdentityUser类
虽然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类的更多相关文章
- ASP.NET Core中的Startup类
ASP.NET Core程序要求有一个启动类.按照惯例,启动类的名字是 "Startup" .Startup类负责配置请求管道,处理应用程序的所有请求.你可以指定在Main方法中使 ...
- 在ASP.Net Core 中使用枚举类而不是枚举
前言:我相信大家在编写代码时经常会遇到各种状态值,而且为了避免硬编码和代码中出现魔法数,通常我们都会定义一个枚举,来表示各种状态值,直到我看到Java中这样使用枚举,我再想C# 中可不可以这样写,今天 ...
- ASP.NET Core 中文文档 第三章 原理(1)应用程序启动
原文:Application Startup 作者:Steve Smith 翻译:刘怡(AlexLEWIS) 校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程 ...
- ASP.NET Core中,UseDeveloperExceptionPage扩展方法会吃掉异常
在ASP.NET Core中Startup类的Configure方法中,有一个扩展方法叫UseDeveloperExceptionPage,如下所示: // This method gets call ...
- ASP.NET Core中的Startup
原文:链接 Startup.cs的作用: 配置各服务和HTTP请求管道. Startup类: ASP.NET Core中使用按惯例Startup命名的类Startup.cs: (可选)包括Config ...
- (1)ASP.NET Core 应用启动Startup类简介
1.前言 Core与早期版本的 ASP.NET 对比,配置应用程序的方式的 Global.asax.FilterConfig.cs和RouteConfig.cs 都被Program.cs 和 Star ...
- 在ASP.NET Core中使用百度在线编辑器UEditor
在ASP.NET Core中使用百度在线编辑器UEditor 0x00 起因 最近需要一个在线编辑器,之前听人说过百度的UEditor不错,去官网下了一个.不过服务端只有ASP.NET版的,如果是为了 ...
- ASP.NET Core中的依赖注入(1):控制反转(IoC)
ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制,ASP.NET通过定义接口的方式对它们进行了"标准化&qu ...
- ASP.NET Core中的依赖注入(2):依赖注入(DI)
IoC主要体现了这样一种设计思想:通过将一组通用流程的控制从应用转移到框架之中以实现对流程的复用,同时采用"好莱坞原则"是应用程序以被动的方式实现对流程的定制.我们可以采用若干设计 ...
随机推荐
- 每秒浮点运算次数flops
每秒浮点运算次数[编辑] 维基百科,自由的百科全书 跳到导航跳到搜索 此条目需要补充更多来源. (2018年2月28日)请协助添加多方面可靠来源以改善这篇条目,无法查证的内容可能会因为异议提出 ...
- IDEA中获取资源路径问题
更正 以src开始,就能用相对路径了... shift+ctrl+alt+s 调出项目结构, 在Modules里,就是设置 Sources Resources Test的界面, 右面的路径就是相对路径 ...
- [linux]ubuntu修改镜像源
sudo apt-get update 更新源 sudo apt-get install package 安装包 sudo apt-get remove package 删除包 sudo apt-ca ...
- Selenium+Python附件上传
在自动化测试过程中,我们会经常遇到附件上传,而附件上传主要分为两种:input型.非input型,我们本章就两种不同类型的上传方式讲解: (1)input型 <input id="tx ...
- Java学习日记基础篇(三-下)——流程控制之循环控制
循环控制 for循环 语法: for(循环初值;循环条件;步长) { 语句; //循环体 } 例子: import java.io.*; public class Demo4 { public sta ...
- SpringCloud学习整理
参考文档 [1]: Spring Cloud Ribbon负载均衡
- little difference
把一个数字分解成有限个相差不超过1的因子: 这里如果是2的n次幂就不可以,因为比如4,可以拆成 2,2,或者2,2,1,或者2,2,1,1,...所有这个不可以,没想到这个 数据是1E18,一开始想觉 ...
- mac使用vnc远程登录ubuntu16.04桌面
mac使用vnc远程登录ubuntu16.04桌面 1.安装在Ubuntu上安装x11vnc,如下: sudo apt-get install x11vnc 2.配置vnc密码 x11vnc -sto ...
- arcgis python ValueTable使用
本文链接:https://blog.csdn.net/A873054267/article/details/86007125 #多值参数指定方式 1 python list类型 2 字符串类型,以逗号 ...
- 连接池设置导致的“血案” 原创: 一页破书 一页破书 5月6日 这个问题被投诉的几个月了,一直没重视——内部客户嘛😿 问题现象: 隔几周就会出现 A服务调用B服务超时 脚趾头想就是防火墙的问题,A、B两服务之间有防火墙 找运维查看防火墙日志确实断掉了tcp连接,但是是因为B服务5分钟没有回包,下面这个表情就是我当时的心情——其实我们在防火墙、A服务、B服务都抓包了,几十个G的t
连接池设置导致的“血案” 原创: 一页破书 一页破书 5月6日 这个问题被投诉的几个月了,一直没重视——内部客户嘛