Authentication是授权,验证,它是用来验证是否登录成功,
Authorization是授权,它是用来判断用户是否有权限访问,它应该是基于Authentication之上的。
使用Identity(标识)框架:
一般的系统中都需要通过数据库保存用户、角色等信息,并且需要注册、登录、密码重置、角色管理等功能,但是ASP.NET Core提供了标识(identity)框架,内置了对用户、角色等表的管理及相关的接口,简化了系统的开发。
1、新建asp.netcore webapi项目,安装Microsoft.AspNetCore.Identity.EntityFrameworkCore
2、创建用户实体类User和角色实体类Role,分别继承自IdentityUser、IdentityRole,主键自增
public class User : IdentityUser<long>
{
public DateTime CreationTime { get; set; }
public string? NickName { get; set; }
}
public class Role : IdentityRole<long>
{
}
3、创建继承自IdentityDbContext的类,EFCore中的上下文类,操作数据库,其中的3个泛型参数是用户类、角色类、主键类型。