您的位置:首页 > 编程语言 > ASP

ASP.NET CORE[练习5]-Identity-扩展、注入服务、Migration(启示篇)

2019-08-31 11:05 1031 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_25991955/article/details/100169963

练习+博客,量化自己的进步!

用户登录、权限校验是一个系统必要的东西,以前的项目都是使用自己搭建的用户角色系统,使用 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进行用户创建,用户校验了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: