Visual
- 从这一讲开始的源码,视频作者杨旭大神都放在了自己的 GitHub 上
- 我也将我学习的代码放到了 GitHub
一句话的事儿
- 如何管理网站用户(CRUD:创建,删除,更新,查询)
- 如何验证用户(前端UI对用户的验证,通过DataAnnotation)
准备工作
- 从 GitHub 下载源码
- 打开 15 start 项目
- 打开程序包管理控制台,更新数据库
Update-Database -Context HeavyContextUpdate-Database -Context ``ApplicationDbContext
使用 Identity—用户管理(IdentityManager)
创建项目
创建项目使用 ASP.NET Core MVC 模板 + 身份认证选择 个人。
自动生成的 Startup 里面的部分代码就启用了 Identity:
public void ConfigureServices(IServiceCollection services){...services.AddDefaultIdentity<IdentityUser>().AddDefaultUI(UIFramework.Bootstrap4).AddEntityFrameworkStores<ApplicationDbContext>();...}public void Configure(IApplicationBuilder app, IHostingEnvironment env){...app.UseCookiePolicy();app.UseAuthentication();...}
UserController
通过依赖注入的 UserManager 实现对 User 的操控。
using System.Threading.Tasks;using Heavy.Web.Models;using Heavy.Web.ViewModels;using Microsoft.AspNetCore.Authorization;using Microsoft.AspNetCore.Identity;using Microsoft.AspNetCore.Mvc;using Microsoft.EntityFrameworkCore;namespace Heavy.Web.Controllers{[Authorize]public class UserController : Controller{private readonly UserManager<ApplicationUser> _userManager;public UserController(UserManager<ApplicationUser> userManager){_userManager = userManager;}/// <summary>/// 获取所有用户/// </summary>/// <returns></returns>public async Task<IActionResult> Index(){var users = await _userManager.Users.ToListAsync();return View(users);}/// <summary>/// 增加一个用户[HttpGet],其目的是防止刷新后反复添加用户,从而用于Post返回/// </summary>/// <returns></returns>public IActionResult AddUser(){return View();}/// <summary>/// 增加一个用户[HttpPost]/// </summary>/// <param name="userAddViewModel">创建用户的UI</param>/// <returns></returns>[HttpPost]public async Task<IActionResult> AddUser(UserCreateViewModel userAddViewModel){//Model验证,若失败返回到Action,即AddUserif (!ModelState.IsValid){return View(userAddViewModel);}//创建用户并赋值相关属性,其中密码使用_userManager.CreateAsync()var user = new ApplicationUser{UserName = userAddViewModel.UserName,Email = userAddViewModel.Email,IdCardNo = userAddViewModel.IdCardNo,BirthDate = userAddViewModel.BirthDate};var result = await _userManager.CreateAsync(user, userAddViewModel.Password);//验证创建是否成功if (result.Succeeded){return RedirectToAction("Index");}//将错误藐视输出foreach (IdentityError error in result.Errors){ModelState.AddModelError(string.Empty, error.Description);}return View(userAddViewModel);}/// <summary>/// 编辑用户[HttpGet]/// </summary>/// <param name="id">用户Id</param>/// <returns></returns>public async Task<IActionResult> EditUser(string id){var user = await _userManager.FindByIdAsync(id);if (user == null){return RedirectToAction("Index");}return View(user);}/// <summary>/// 编辑用户[HttpPost]/// </summary>/// <param name="id">用户Id</param>/// <param name="userEditViewModel">编辑用户的UI</param>/// <returns></returns>[HttpPost]public async Task<IActionResult> EditUser(string id, UserEditViewModel userEditViewModel){var user = await _userManager.FindByIdAsync(id);if (user == null){return RedirectToAction("Index");}user.UserName = userEditViewModel.UserName;user.Email = userEditViewModel.Email;user.IdCardNo = userEditViewModel.IdCardNo;user.BirthDate = userEditViewModel.BirthDate;var result = await _userManager.UpdateAsync(user);if (result.Succeeded){return RedirectToAction("Index");}ModelState.AddModelError(string.Empty, "更新用户信息时发生错误");return View(user);}/// <summary>/// 删除用户[HttpPost]/// </summary>/// <param name="id"></param>/// <returns></returns>[HttpPost]public async Task<IActionResult> DeleteUser(string id){var user = await _userManager.FindByIdAsync(id);if (user != null){var result = await _userManager.DeleteAsync(user);if (result.Succeeded){return RedirectToAction("Index");}ModelState.AddModelError(string.Empty, "删除用户时发生错误");}else{ModelState.AddModelError(string.Empty, "用户找不到");}return View("Index", await _userManager.Users.ToListAsync());}}}
UserXxViewModel
User 的各种视图模型,通过特性标注实现基本的验证和显式设置:
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Linq;using System.Threading.Tasks;namespace Heavy.Web.ViewModels{/// <summary>/// 创建User的前端UI/// </summary>public class UserCreateViewModel{[Required][Display(Name = "用户名")]public string UserName { get; set; }//[Required][DataType(DataType.EmailAddress)][RegularExpression(@"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|""(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*"")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])", ErrorMessage = "Email的格式不正确")]public string Email { get; set; }[Required][DataType(DataType.Password)]public string Password { get; set; }[Required][Display(Name = "身份证号")][StringLength(18, MinimumLength = 18, ErrorMessage = "{0}的长度是{1}")]public string IdCardNo { get; set; }[Required][Display(Name = "出生日期")][DataType(DataType.Date)]public DateTime BirthDate { get; set; }}}
扩展 IdentityUser
查看 IdentityUser 的源码,不难发现它的属性并不多。我们可以通过继承它来创建属性更丰富的 IdentityUser。
Identity源码
#region Assembly Microsoft.Extensions.Identity.Stores, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60// C:\Users\Felix\.nuget\packages\microsoft.extensions.identity.stores\2.2.0\lib\netstandard2.0\Microsoft.Extensions.Identity.Stores.dll#endregionusing System;namespace Microsoft.AspNetCore.Identity{//// Summary:// Represents a user in the identity system//// Type parameters:// TKey:// The type used for the primary key for the user.public class IdentityUser<TKey> where TKey : IEquatable<TKey>{//// Summary:// Initializes a new instance of Microsoft.AspNetCore.Identity.IdentityUser`1.public IdentityUser();//// Summary:// Initializes a new instance of Microsoft.AspNetCore.Identity.IdentityUser`1.//// Parameters:// userName:// The user name.public IdentityUser(string userName);//// Summary:// Gets or sets the date and time, in UTC, when any user lockout ends.//// Remarks:// A value in the past means the user is not locked out.public virtual DateTimeOffset? LockoutEnd { get; set; }//// Summary:// Gets or sets a flag indicating if two factor authentication is enabled for this// user.//// Value:// True if 2fa is enabled, otherwise false.[PersonalData]public virtual bool TwoFactorEnabled { get; set; }//// Summary:// Gets or sets a flag indicating if a user has confirmed their telephone address.//// Value:// True if the telephone number has been confirmed, otherwise false.[PersonalData]public virtual bool PhoneNumberConfirmed { get; set; }//// Summary:// Gets or sets a telephone number for the user.[ProtectedPersonalData]public virtual string PhoneNumber { get; set; }//// Summary:// A random value that must change whenever a user is persisted to the storepublic virtual string ConcurrencyStamp { get; set; }//// Summary:// A random value that must change whenever a users credentials change (password// changed, login removed)public virtual string SecurityStamp { get; set; }//// Summary:// Gets or sets a salted and hashed representation of the password for this user.public virtual string PasswordHash { get; set; }//// Summary:// Gets or sets a flag indicating if a user has confirmed their email address.//// Value:// True if the email address has been confirmed, otherwise false.[PersonalData]public virtual bool EmailConfirmed { get; set; }//// Summary:// Gets or sets the normalized email address for this user.public virtual string NormalizedEmail { get; set; }//// Summary:// Gets or sets the email address for this user.[ProtectedPersonalData]public virtual string Email { get; set; }//// Summary:// Gets or sets the normalized user name for this user.public virtual string NormalizedUserName { get; set; }//// Summary:// Gets or sets the user name for this user.[ProtectedPersonalData]public virtual string UserName { get; set; }//// Summary:// Gets or sets the primary key for this user.[PersonalData]public virtual TKey Id { get; set; }//// Summary:// Gets or sets a flag indicating if the user could be locked out.//// Value:// True if the user could be locked out, otherwise false.public virtual bool LockoutEnabled { get; set; }//// Summary:// Gets or sets the number of failed login attempts for the current user.public virtual int AccessFailedCount { get; set; }//// Summary:// Returns the username for this user.public override string ToString();}}
添加了身份证号和出生日期的 ApplicationUser:
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Identity;namespace Heavy.Web.Models{public class ApplicationUser: IdentityUser{//添加身份证号码,并通过DataAnnotations验证[MaxLength(18)]public string IdCardNo { get; set; }//添加出生日期,并通过DataAnnotations验证[DataType(DataType.Date)]public DateTime BirthDate { get; set; }}}
然后将 Configure Services 里面的代码:services.AddDefaultIdentity<IdentityUser> 修改为 services.AddDefaultIdentity<ApplicationUser>
然后修改 ApplicationDbContext 指明 IdentityUser 的实现类为 ApplicationUser:
此处的原因是IdentityUser的继承类ApplicationUser是来自于IdentityDbContext,与此同时也能够让ApplicationUser能够被EF Core下的DbContext追踪。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>{...}
然后 Add-Migration + Update-Database 更新数据库。
最后将程序中所有原来使用 IdentityUser 的地方替换为 ApplicationUser。
自定义密码规则
Identity 默认要求用户设置复杂的强密码,我们可以通过 IdentityOptions 自定义密码规则。
services.AddDefaultIdentity<ApplicationUser>(options =>{options.Password.RequireNonAlphanumeric = false;options.Password.RequireLowercase = false;options.Password.RequireUppercase = false;options.Password.RequiredLength = 6;}).AddDefaultUI(UIFramework.Bootstrap4).AddEntityFrameworkStores<ApplicationDbContext>();
补充官网源码
https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/identity-configuration?view=aspnetcore-3.1
锁定—Lockout
Identity 选项。锁定将指定LockoutOptions ,其中包含表中所示的属性。
表 2
| 属性 | 说明 | 默认 |
|---|---|---|
| AllowedForNewUsers | 确定新用户是否可以锁定。 | true |
| DefaultLockoutTimeSpan | 锁定发生时用户被锁定的时间长度。 | 5 分钟 |
| MaxFailedAccessAttempts | 如果启用了锁定,则在用户被锁定之前失败的访问尝试次数。 | 5 |
密码—Password
Identity Options. Password指定PasswordOptions ,其中包含表中所示的属性。
表 3
| 属性 | 说明 | 默认 |
|---|---|---|
| RequireDigit | 要求密码中的数字介于0-9 之间。 | true |
| RequiredLength | 密码的最小长度。 | 6 |
| RequireLowercase | 密码中需要小写字符。 | true |
| RequireNonAlphanumeric | 密码中需要一个非字母数字字符。 | true |
| RequiredUniqueChars | 仅适用于 ASP.NET Core 2.0 或更高版本。 需要密码中的非重复字符数。 |
1 |
| RequireUppercase | 密码中需要大写字符。 | true |
登录—SignIn
“ Identity 登录“ 指定SignInOptions ,其中包含表中所示的属性。
登录
| 属性 | 说明 | 默认 |
|---|---|---|
| RequireConfirmedEmail | 需要确认电子邮件登录。 | false |
| RequireConfirmedPhoneNumber | 需要确认电话号码才能登录。 | false |
令牌—Token
Identity 选项。标记指定TokenOptions ,其中包含表中所示的属性。
令牌牌
| 属性 | 说明 |
|---|---|
| AuthenticatorTokenProvider | 获取或设置 AuthenticatorTokenProvider 用于使用验证器验证双重登录的。 |
| ChangeEmailTokenProvider | 获取或设置 ChangeEmailTokenProvider 用于生成电子邮件更改确认电子邮件中使用的令牌的。 |
| ChangePhoneNumberTokenProvider | 获取或设置 ChangePhoneNumberTokenProvider 用于生成更改电话号码时使用的令牌的。 |
| EmailConfirmationTokenProvider | 获取或设置用于生成帐户确认电子邮件中使用的令牌的令牌提供程序。 |
| PasswordResetTokenProvider | 获取或设置用于生成密码重置电子邮件中使用的令牌的IUserTwoFactorTokenProvider |
| ProviderMap | 用于使用用作提供程序名称的密钥构造 用户令牌提供程序 。 |
用户—User
Identity Options。 User指定UserOptions ,其中包含表中所示的属性。
用户
| 属性 | 说明 | 默认 |
|---|---|---|
| AllowedUserNameCharacters | 用户名中允许使用的字符。 | abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 -._@+ |
| RequireUniqueEmail | 要求每个用户都有唯一的电子邮件。 | false |
Cookie 设置
services.ConfigureApplicationCookie(options =>{options.AccessDeniedPath = "/Identity/Account/AccessDenied";options.Cookie.Name = "YourAppCookieName";options.Cookie.HttpOnly = true;options.ExpireTimeSpan = TimeSpan.FromMinutes(60);options.LoginPath = "/Identity/Account/Login";// ReturnUrlParameter requires//using Microsoft.AspNetCore.Authentication.Cookies;options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;options.SlidingExpiration = true;});
Password Hasher 选项
PasswordHasherOptions 获取和设置用于密码哈希的选项。
PASSWORD HASHER 选项
| 选项 | 说明 |
|---|---|
| CompatibilityMode | 对新密码进行哈希处理时使用的兼容性模式。 默认为 IdentityV3。 哈希密码的第一个字节称为 格式标记,它指定用于对密码进行哈希处理的哈希算法的版本。 针对哈希验证密码时,该方法会根据 VerifyHashedPassword 第一个字节选择正确的算法。 无论使用哪个版本的算法对密码进行哈希处理,客户端都可以进行身份验证。 设置兼容性模式会影响 新密码的哈希。 |
| IterationCount | 使用 PBKDF2 对密码进行哈希处理时使用的迭代次数。 仅当设置为时,才使用此值 CompatibilityMode IdentityV3 。 该值必须是正整数并且默认值为 10000 。 |

