首页 > 其他分享 >netcore Identity(一)

netcore Identity(一)

时间:2023-07-20 11:59:01浏览次数:35  
标签:netcore IdentityUser 用户 添加 Microsoft Identity AppUser

描述

asp.net Core Identity提供给我们一组工具包和API,能帮助我们应用程序创建授权和认证功能。也可以用它创建账户并使用用户名和密码进行登录,同时也提供了角色和角色管理功能。

1.创建项目

  • 配置项
    • nuget包
      • Microsoft.AspNetCore.Identity.EntityFrameWorkCore
      • Microsoft.EntityFrameworkCore.Design
      • Microsoft.EntityFrameworkCore.SqlServer
        image

  • 配置项目
Program.cs
app.UseAuthorization();
app.UseAuthorization();

  • 设置Asp.net Core Identity

    User类
      		User类继承IdentityUser类,位于Microsoft.AspNetCore.Identity中
      	在Models文件夹中穿件AppUser类
      		IdentityUser类中提供了一些用户属性,如:用户名、电子邮件、电话、密码hash值等。
      	如果IdentityUser类不能满足要求,可以在AppUser中添加自定义的属性
    

IdentityUser常用属性

名称 描述
ID 用户唯一ID
UserName 用户名称
Email 用户Email
PasswordHash 用户密码Hash的值
PhoneNumber 用户电话号码
SecurityStamp 当每次用户的数据修改时生成随机值
创建Database Context
DataBase Context类继承IdentityDbContext<T>类,T表示User类,在应用程序中使用AppUser,IdentityDbContext通过使用EntityFrameworkCore和数据库进行交互
AppIdentyDbContext继承IdentityDbContext
namespace IdentityDemo1.Models
{
    public class AppIdentityDbContext : IdentityDbContext<AppUser>
    {
        public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options) : base(options)
        {

        }
    }
}
创建数据库字符串连接

appsettings.json中配置

appsettings.json中配置数据库连接字符串
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "ConnectionStrings": {
    "Default": "Data Source=.;Initial Catalog=IdentityDemo;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=true"
  },
  "AllowedHosts": "*"
}

在AddDbContext()方法中添加AppIdentityDbContext类并且指定它使用SqlServer数据库,连接字符串从配置文件中读取
点击查看代码
builder.Services.AddDbContext<AppIdentityDbContext>(opt =>
{
    opt.UseSqlServer(builder.Configuration["ConnectionStrings:Default"]);
});
添加Asp.Net Identity服务
添加Asp.Net Identity服务
builder.Services.AddIdentity<AppUser, IdentityRole>()
    .AddEntityFrameworkStores<AppIdentityDbContext>()
    .AddDefaultTokenProviders();
- AddIdentity方法的参数类型指定AppUser类和IdentityRole类 - AddEntityFrameworkStore方法指定Identity使用EF作为存储和项目中使用AppIdentityContext作为Db Context - AddDefaultTokenProviders方法添加默认Token提供程序,针对重置密码,电话号码和邮件变更操作以及生成双因子认证的token - 添加了app.UseAuthentication()方法,经过这个方法的每个http请求将用户的凭据添加到Cookie中或URL中,这使得用户和它发送的http请求会产生关联。
使用EF Migration命令创建Identity数据库
nuget命令
nuget EntityFrameworkCore.Tool
Add-Migration InitCreateDB
update-database

执行后的结果是
image
包含用户记录,角色,Claims,token 和登录次数详细信息等。

  • __EFMigrationsHistory:包含了前面所有的Migration
  • AspNetRoleClaims :按角色存储Claims
  • AspNetRoles:存储所有角色
  • AspNetUserClaims :存储用户的Claims
  • AspNetUserLogins :存储用户的登录次数
  • AspNetUserRoles: 存储用户的对应的角色
  • AspNetUsers:存储用户
  • AspNetUserTokens 存储外部认证的token

标签:netcore,IdentityUser,用户,添加,Microsoft,Identity,AppUser
From: https://www.cnblogs.com/xiaoxi888/p/17567738.html

相关文章

  • Cisco Identity Services Engine (ISE) 3.3 - 思科身份服务引擎
    CiscoIdentityServicesEngine(ISE)3.3-思科身份服务引擎请访问原文链接:https://sysin.org/blog/cisco-ise-3/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgCisco现已发布ISE3.3。思科身份服务引擎(ISE)了解和控制网络上的设备和用户利用整个堆栈中的......
  • netcore控制反转
    简介IoC Ioc控制反转,是一种设计模式和原则,旨在解耦组件之间的依赖关系,并将对象的创建和管理委托外部容器。是面向编程中一种重要的概念,用于提高代码的可维护性. 核心思想:通过将控制权从高层转移到底层模块,实现对依赖关系的控制反转,传统上,一个对象通常负责自己的依赖项创建和管......
  • netcore模型配置
    模型配置可以通过FluentAPI和注解的方式FluentAPI步骤新建Products和Category类新建Products类ProductspublicclassProduct{publicintId{get;set;}publicstringName{get;set;}publicdecimalPrice{get;set;}......
  • 【Azure API Management】实现在API Management服务中使用MI(管理标识 Managed Identi
    问题描述在Azure的同一数据中心,APIManagement访问启用了防火墙的StorageAccount,并且把APIM的公网IP地址设置在白名单。但访问依旧是403原因是:存储帐户部署在同一区域中的服务使用专用的AzureIP地址进行通信。因此,不能基于特定的Azure服务的公共出站IP地址范围来限制......
  • 使用列表并且 IDENTITY_INSERT 为 ON 时,才能为表
    原因:因为表中含有自增标识,无法直接为制定的序号做插入操作,需要更改标识(先开启,执行后SQL后,在关上)setidentity_insert  C_User_Registeron--设置标识列可以显示添加数据insertintoC_User_Register(PCId,PCUse,PCNote) values(100,1,'aaa')setidentity_insert C_User_Regist......
  • Authentication With ASP.NET Core Identity
    AuthenticationWithASP.NETCoreIdentity、PreparingtheAuthenticationEnvironmentinourProjectThefirstthing,wearegoingtodoisdisableunauthorizeduserstoaccesstheEmployeesaction.Todothat,wehavetoaddthe[Authorize]attributeonto......
  • User Registration with ASP.NET Core Identity
    UserRegistrationwithASP.NETCoreIdentitySobasically,wehavealltheinputfieldsfromourmodelinthisview.Ofcourse,clickingtheCreatebuttonwilldirectustothePOSTRegistermethodwiththe UserRegistrationModel populated.Now,let’sinst......
  • asp.net core identity tables
      SELECT*FROMdbo.AspNetUsers--hasdataSELECT*FROMdbo.AspNetUserLoginsSELECT*FROMdbo.AspNetUserRoles--hasdataSELECT*FROMdbo.AspNetUserTokensSELECT*FROMdbo.AspNetUserClaimsSELECT*FROMdbo.AspNetRoles--hasdataSELECT*FROMdbo.......
  • Microsoft.AspNetCore.Http.Abstractions 2.20 is deprecated
     您想要升级Microsoft.AspNetCore.Http.Abstractions包,您需要注意以下几点:Microsoft.AspNetCore.Http.Abstractions包在ASP.NETCore2.2版本后已经被标记为过时,因为它已经被包含在Microsoft.AspNetCore.App框架引用中12。因此,您不需要单独引用这个包,只需要在项目文件中......
  • aspnetcore 中间件执行顺序
    这是用例和返回结果输出的结果是对称的当我第一眼看着这个操作时满脑子不解:一个方法是怎么扳成2截来使用的要是我来做肯定让用户传2个委托完整实现代码classProgram{staticList<Action<Action>>middlewareList=newList<Action<Action>>();staticvoidUse(......