.NET Core 之Identity认证
.认证
前言
这里主要通过示例使用ASP.NET Core Identity,从Visual Studio中的ASP.NET Core Identity模板创建一个新项目。
环境
VS2019 +.NET5.0+Microsoft.AspNetCore.Identity.EntityFrameworkCore5.0
创建项目
创建项目,选择“ASP.NET Core Web 应用”。
身份验证类型设置为“个人账户”。
命名为“WebAPP1”
因为创建项目的时候选择了个人账户,所以默认是基于 Microsoft.EntityFrameworkCore 的 ORM 框架来操作数据库的。
微软已经定义好了一个可扩展的 DbContext 对象,默认命名叫 ApplicationDbContext。
依次打开【Data -> Migrations -> ApplicationDbContext】,代码如下:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions
: base(options)
{
}
}
父类 IdentityDbContext :
//
// 摘要:
// Base class for the Entity Framework database context used for identity.
public class IdentityDbContext : IdentityDbContext
{
//
// 摘要:
// Initializes a new instance of Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext.
//
// 参数:
// options:
// The options to be used by a Microsoft.EntityFrameworkCore.DbContext.
public IdentityDbContext(DbContextOptions options);
//
// 摘要:
// Initializes a new instance of the Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext
// class.
protected IdentityDbContext();
}
继承了 IdentityDbContext 基类,还定义了泛型的类型,IdentityUser 和 IdentityRole 就是用户和角色实体。但还是没有 DbSet 属性,所以我们接着看下泛型基类IdentityDbContext:
//
// 摘要:
// Base class for the Entity Framework database context used for identity.
//
// 类型参数:
// TUser:
// The type of user objects.
//
// TRole:
// The type of role objects.
//
// TKey:
// The type of the primary key for users and roles.
public class IdentityDbContext
where TUser : IdentityUser
where TRole : IdentityRole
where TKey : IEquatable
{
//
// 摘要:
// Initializes a new instance of the db context.
//
// 参数:
// options:
// The options to be used by a Microsoft.EntityFrameworkCore.DbContext.
public IdentityDbContext(DbContextOptions options);
//
// 摘要:
// Initializes a new instance of the class.
protected IdentityDbContext();
}
又做了一次封装:
public abstract class IdentityDbContext
where TUser : IdentityUser
where TRole : IdentityRole
where TKey : IEquatable
where TUserClaim : IdentityUserClaim
where TUserRole : IdentityUserRole
where TUserLogin : IdentityUserLogin
where TRoleClaim : IdentityRoleClaim
where TUserToken : IdentityUserToken
{
//
// 摘要:
// Initializes a new instance of the class.
//
// 参数:
// options:
// The options to be used by a Microsoft.EntityFrameworkCore.DbContext.
public IdentityDbContext(DbContextOptions options);
//
// 摘要:
// Initializes a new instance of the class.
protected IdentityDbContext();
//
// 摘要:
// Gets or sets the Microsoft.EntityFrameworkCore.DbSet`1 of User roles.
public virtual DbSet
//
// 摘要:
// Gets or sets the Microsoft.EntityFrameworkCore.DbSet`1 of roles.
public virtual DbSet
//
// 摘要:
// Gets or sets the Microsoft.EntityFrameworkCore.DbSet`1 of role claims.
public virtual DbSet
//
// 摘要:
// Configures the schema needed for the identity framework.
//
// 参数:
// builder:
// The builder being used to construct the model for this context.
protected override void OnModelCreating(ModelBuilder builder);
}
在使用 ApplicationDbContext 时,可以对用户和角色以及相关的 DbSet 进行操作了。
连接数据库
这里默认是SQLServer数据库,打开appsettings文件,修改数据库连接字符串。
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=IdentityTest;Trusted_Connection=True;MultipleActiveResultSets=true"
}
在包管理器控制台中运行以下命令 (PMC) :
PM> Update-Database |
打开数据库
运行程序
注册
总结
如果需要一个新的用户数据库,那么ASP.NET Core Identity是一个选择。 以后会介绍如何将ASP.NET Core Identity 和 IdentityServer4一起使用。
本篇简单地介绍了一些Identity的身份框架的使用,当然,由于本人的水平有限,因此其中的一些更加深层的例如注册授权策略并未详细的描述。通过Identity框架,我们可以很轻松地进行对用户的登录、注册一系列操作进行管理和控制,确保了网站的安全性和解耦性。
鸣谢:
https://blog.csdn.net/qq_37025471/article/details/103412862?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-3.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-3.pc_relevant_default&utm_relevant_index=6
https://www.cnblogs.com/choii/p/13915892.html
https://www.cnblogs.com/stulzq/p/8120129.html
https://blog.csdn.net/playermaker57/article/details/96713891
https://segmentfault.com/a/1190000014994859
https://blog.51cto.com/u_12517860/4289913
https://blog.csdn.net/slowlifes/article/details/79491104
https://blog.csdn.net/qq_37025471/article/details/103412862?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-3.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-3.pc_relevant_default&utm_relevant_index=6
https://blog.csdn.net/willianyy/article/details/108273347?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&utm_relevant_index=2
https://www.cnblogs.com/qtiger/p/14593169.html