ASP.NET CORE[练习5]-Identity-扩展、注入服务、Migration(启示篇)
练习+博客,量化自己的进步!
用户登录、权限校验是一个系统必要的东西,以前的项目都是使用自己搭建的用户角色系统,使用 Filter 校验。这次学习使用 .net core 内置的身份验证。
.net core 的身份验证有 Cookie 身份验证和持有者身份令牌验证,这里做的 web 项目就用基于浏览器的 Cookie 身份验证。
继承 .net core 身份验证有两个核心类:
UserManager< IdentityUer >
操作用户的类, IdentityUser 是拓展必须继承的用户基础实体类。
SignInManager< IdentityUser >
用来身份认证的类。
1.自定义一个用户实体类继承 IdentityUser
public class ApplicationUser: IdentityUser // IdentityUser 必须引用 Microsoft.AspNetCore.Identity; { public ApplicationUser() { Claims = new List<IdentityUserClaim<string>>(); Logins = new List<IdentityUserLogin<string>>(); Tokens = new List<IdentityUserToken<string>>(); UserRoles = new List<IdentityUserRole<string>>(); } // 在 IdentityUser 的基础上拓展两个字段属性。 public string NickName { get; set; } public int Gender { get; set; } // 以下四个属性是 Identity 内置的四个权限属性,照着添加上去再权限校验的时候用到。 public ICollection<IdentityUserClaim<string>> Claims { get; set; } public ICollection<IdentityUserLogin<string>> Logins { get; set; } public ICollection<IdentityUserToken<string>> Tokens { get; set; } public ICollection<IdentityUserRole<string>> UserRoles { get; set; } }
NickName 、Gender 两个拓展字段,就创建到表中,下面的四个导航属性,会创建成表到数据库中。
2.自定义一个身份验证 DbContext ,继承 IdentityDbContext
IdentityDbContext 包含有 UserManager 、 SigninManager 服务。
public class ApplicationUserDbContext:IdentityDbContext<ApplicationUser> { public ApplicationUserDbContext(DbContextOptions<ApplicationUserDbContext> options) : base(options) { } // 用于身份框架的配置,例如表关联,主、外键的配置,都在这个重写方法中配置 protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); // 在自定义 ApplicationUser 类中已经添加了如下字段属性,在这里做关联配置 builder.Entity<ApplicationUser>(b => { // Each User can have many UserClaims b.HasMany(e => e.Claims) .WithOne() .HasForeignKey(uc => uc.UserId) .IsRequired(); // Each User can have many UserLogins b.HasMany(e => e.Logins) .WithOne() .HasForeignKey(ul => ul.UserId) .IsRequired(); // Each User can have many UserTokens b.HasMany(e => e.Tokens) .WithOne() .HasForeignKey(ut => ut.UserId) .IsRequired(); // Each User can have many entries in the UserRole join table b.HasMany(e => e.UserRoles) .WithOne() .HasForeignKey(ur => ur.UserId) .IsRequired(); }); } }
需要引用Microsoft.AspNetCore.Identity.EntityFrameworkCore;
3. startup 注入服务,配置用户密码约束
services.AddDbContext<ApplicationUserDbContext>(options => { options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"), // 第一个参数为数据库连接字符串 b => b.MigrationsAssembly("Test.Management.core") // 第二个参数将Migration创建到这个工程下 ); }); // 为了开发测试方便,这些约束都设置为最简单的 services.AddIdentity<ApplicationUser, IdentityRole>(options => { options.Password.RequireDigit = false; // 要求包含数字 options.Password.RequireLowercase = false; // 要求包含小写字母 options.Password.RequireNonAlphanumeric = false; // 要求包含特殊字符 options.Password.RequireUppercase = false; // 要求包含大写字母 options.Password.RequiredLength = 1; // 密码最小长度,默认为6 options.Password.RequiredUniqueChars = 1; // 唯一字符的数目 }).AddEntityFrameworkStores<ApplicationUserDbContext>();
注意AddEntityFrameworkStores,这个很关键!
4. Migration
前面拓展了 IdentityUser 类,且自定义了身份验证 DbContext ,迁移后将会创建用户表以及一些权限表。
程序包管理控制台执行如下代码:
Add-Migration CreateUserDbContext -context ApplicationUserDbContext
CreateUserDbContext 是创建Migration的文件名,ApplicationUserDbContext是自定义的DbContext。
Update-Database -context ApplicationUserDbContext
执行更新数据库。
执行成功之后效果如下:
创建了Migraton文件。
数据库创建了表。
IdentityUser 拓展的两个字段在用户表中创建了。
出现的问题:
在创建Migration文件的时候如果出现出现如上问题:
1.出现1处问题,是因为没有指定哪一个DbContext,因为在我们之前做学生业务的时候已经自定过DbContext,所以要指定。
2.在2处又出现问题,虽然指定了DbContext,但因为我在startup注入service时指定将Migration创建到Test.Managment.core工程下,所以要在4出切换默认项目。
3.在3处成功执行。
同样Update-Database时也要选好默认项目,和指定DbContext。
总结:
这一篇做好了继承.net core的身份验证系统,下面就可以使用UserManager 、SignInManager进行用户创建,用户校验了。
- ASP.NET Core身份认证服务框架IdentityServer4(2)-整体介绍
- ASP.NET CORE[练习8]-Identity-用户角色、给用户分配角色、角色校验
- asp.net core 系列 3 依赖注入服务
- 【ASP.NET Core】依赖注入高级玩法——如何注入多个服务实现类
- Asp.Net Core 项目实战之权限管理系统(4) 依赖注入、仓储、服务的多项目分层实现
- ASP.NET CORE[练习1]-创建ASP.NET CORE项目以及Migration数据库
- ASP.NET CORE[练习6]-Identity-用户管理
- ASP.NET CORE[练习7]-Identity-用户登录、最简单的登录验证
- ASP.NET Core的身份认证框架IdentityServer4--(3)令牌服务配置访问控制跟UI添加
- (10)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- Ocelot+Identity Server
- ASP.NET Core中的依赖注入(3): 服务的注册与提供
- asp.net core 系列 4 注入服务的生存期
- ASP.NET CORE[练习10]-Identity-自定义Policy
- ASP.NET Core的身份认证框架IdentityServer4--(1)服务配置
- ASPNETCOREAPI 跨域处理 SQL 语句拼接 多条件分页查询 ASPNET CORE 核心 通过依赖注入(注入服务)
- ASP.NET Core依赖注入多个服务实现类
- (7)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- 利用Polly+AOP+依赖注入封装的降级框架
- ASP.NET Core利用原生服务控制反转注入泛型类
- ASP.NET Core开发-获取所有注入(DI)服务
- ASP.NET Core中的依赖注入(2):依赖注入(DI)