Identity – Introduction & Scaffold
主要参考:
Introduction to Identity on ASP.NET Core
Start by command
- dotnet new webapp --auth Individual -uld -o WebApp1
new webapp 是做一个 razor page
--auth Individual 表示要包含 Identity Framework (简称 Identity)
-uld 是 use local database (本地数据库, 默认是用 SQLite)
-o WebApp1 是 output 项目名字
运行上面 command 后, 所有代码就写好了. 它是搭配 EF Core 的. 所以接下来通过 EF Migration 创建数据库.
- dotnet ef database update
在 privacy 页面放上 [Authorize] 标签就可以跑了.
Scaffold Identity
参考: Scaffold Identity in ASP.NET Core projects
之所以几行 command 就可以跑起来, 那是因为 ASP.NET Core 对 Identity 做了一个封装, 这个封装还包括了 View (这个技术叫 Razor Class Library 简称 RCL)
我们来从一个 Web App 没有 authen 的做起.
先装好 global 工具
- dotnet tool install -g dotnet-aspnet-codegenerator
- // 做一个新项目, 没有 authentication 的
- dotnet new webapp -o ScaffoldIdentity
- // 进入项目 folder
- cd ScaffoldIdentity
- // 装 package
- dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
- dotnet add package Microsoft.EntityFrameworkCore.Design
- dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
- dotnet add package Microsoft.AspNetCore.Identity.UI
- dotnet add package Microsoft.EntityFrameworkCore.SqlServer
- dotnet add package Microsoft.EntityFrameworkCore.Tools
- // generate Identity code template
- dotnet aspnet-codegenerator Identity --useDefaultUI
- // 做数据库
- dotnet ef migrations add CreateIdentitySchema
- dotnet ef database update
跑完上面的 command 后, 在 startup 添加 UseAuthentication
然后 layout view 添加 _LoginPartial, 就可以了.
当然这个只是演示让, 让我们知道它是怎样 work 的, 真实开发还是差很多的. 之后慢慢讲.
这里有几个 point 可以记入一下.
在 IdentityHostingStartup.cs 里面封装了 service 的调用, 所以我们在 startup 看不到,
然后所有的 view 都是看不见 source code 的.
除非在我们声明时加入 --files (把指定要的 file 调出来) 或者拿掉 --useDefaultUI 那么所有的 source code 才会显示出来 (这正是我们需要的, 不然怎么魔改呢?)
Identity Overview
既然 Scaffold 帮我们搞了这么多代码, 那我们一个一个来看看 Identity 的代码是如何工作的。要魔改自然要先摸清楚它底细.
首先是 Entity
- public class ScaffoldIdentityIdentityDbContext : IdentityDbContext<IdentityUser>
- {
- public ScaffoldIdentityIdentityDbContext(DbContextOptions<ScaffoldIdentityIdentityDbContext> options)
- : base(options)
- {
- }
- protected override void OnModelCreating(ModelBuilder builder)
- {
- base.OnModelCreating(builder);
- }
- }
把平常的 DbContext 继承了 IdentityDbContext
Identity 封装了许多的 Entity, 比如 IdentityUser, IdentityRole, IdentityLogin 等等, 有了这些, 数据库就搞定了.
AspNetUsers = user table
AspNetUserClaims = user 的 claims
AspNetUserLogins = user 的 external logins
AspNetRoles = role table
AspNetRoleClaims = role 的 claims
AspNetUserRoles = user 和 role 的关系
AspNetUserTokens = 暂时不清楚
Role 和 Claims 都是用来做 autho 的, 之后会讲到
接下来就是 Startup 的 service provide
- services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
- .AddEntityFrameworkStores<ScaffoldIdentityIdentityDbContext>();
源码在这里
2 年前我超讨厌看 .NET 源码, 东一个西一个, 幸好现在全部放一起了.
这些源码最好都稍微看一下,因为大部分情况我们是需要修改的.
AddDefaultIdentity
IdentityServiceCollectionUIExtensions.cs
services.AddAuthentication 是 ASP.NET Core authen 的 service 哦
AddIdentityCookies
IdentityCookiesBuilderExtensions.cs
里面负责了 cookie schemes 的代码
IdentityServiceCollectionExtensions.cs
负责UserManger 之类的
AddDefaultUI
IdentityBuilderUIExtensions.cs
负责 Ui 的咚咚, SignInManager 也在这里
AddDefaultTokenProviders
Email, Phone reset password 这些 token 的做法
还有一个 AddIdentity
IdentityServiceCollectionExtensions.cs
这个是以前还没有 AddDefaultIdentity 的时候的调用, 如果现在没有要 DefaultUI 也是会用回 AddIdentity + AddDefaultTokenProviders
注: AddDefaultUI 会有 Error Page 哦, 可能破坏 router, 除非你真的要, 不然建议不适用它.
整个过程就是, 你要什么就拿什么来用, 或拿来改就是了, 最重要的是全部过一遍, 知道它都搞了些什么.
Identity – Introduction & Scaffold的更多相关文章
- 000.Introduction to ASP.NET Core--【Asp.net core 介绍】
Introduction to ASP.NET Core Asp.net core 介绍 270 of 282 people found this helpful By Daniel Roth, Ri ...
- Discrete.Differential.Geometry-An.Applied.Introduction(sig2013) 笔记
The author has a course on web: http://brickisland.net/DDGSpring2016/ It has more reading assignment ...
- A.Kaw矩阵代数初步学习笔记 1. Introduction
“矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...
- Security » Authentication » Identity介绍
Introduction to Identity¶ By Pranav Rastogi, Rick Anderson, Tom Dykstra, Jon Galloway and Erik Reita ...
- [转]DCM Tutorial – An Introduction to Orientation Kinematics
原地址http://www.starlino.com/dcm_tutorial.html Introduction This article is a continuation of my IMU G ...
- TIJ——Chapter One:Introduction to Objects
///:~容我对这个系列美其名曰"读书笔记",其实shi在练习英文哈:-) Introduction to Objects Object-oriented programming( ...
- Core Java Volume I — 4.1. Introduction to Object-Oriented Programming
4.1. Introduction to Object-Oriented ProgrammingObject-oriented programming, or OOP for short, is th ...
- Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)
1, http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c 2, Introduction ...
- Introduction to the POM
原文:https://maven.apache.org/guides/introduction/introduction-to-the-pom.html Introduction to the POM ...
- asp.net core系列 46 Identity介绍
一. Identity 介绍 ASP.NET Core Identity是一个会员系统,可为ASP.NET Core应用程序添加登录功能.可以使用SQL Server数据库配置身份以存储用户名,密码和 ...
随机推荐
- Grafana Loki查询加速:如何在不添加资源的前提下提升查询速度
Grafana Loki查询加速:如何在不添加资源的前提下提升查询速度 来自Grafana Loki query acceleration: How we sped up queries withou ...
- 奇思妙想,动手 DIY 你的浏览器主页
实战开发和上线一个极客范儿的浏览器主页,原来如此简单! 大家好我是鱼皮,前段时间上线了一个程序员必备的浏览器主页,得到了很多同学的好评. 地址:https://home.code-nav.cn/ 其实 ...
- Vue3中如何使用this
vue3提供了getCurrentInstance ,通过这个属性,直接使用ctx是错误的,需要找到全局属性globalProperties import { getCurrentInstance } ...
- ABC350
A link 把最后三位取成数字,判断是否小于\(349\),大于\(1\),不等于\(316\). 点击查看代码 #include<bits/stdc++.h> using namesp ...
- 单细胞测序最好的教程(九): 细胞类型自动注释|发表在Science的注释算法
作者按 本章节主要讲解了基于大模型的自动注释方法,包括CellTypist(发表在Science)和MetaTiME(发表在Nature communication),一个通用,一个泛癌专用.本教程首 ...
- 错误记录java: JDK isn't specified for module
跑苍穹外卖的时候遇到了 java: JDK isn't specified for module 'sky-pojo'这一问题 解决办法是通过修改JDK版本,这个项目用的springboot比较早,可 ...
- app专项测试:app弱网测试(测试工具)
app专项测试:app弱网测试(测试工具) 除了常用的 fiddler,charles 可以模拟弱网,还有硬件工具弱网仪 HoloWAN也可以模拟弱网 使用弱网仪有以下优点:1.即插即用,无需调试和复 ...
- 【MySQL】编写随机密码生成脚本
数据需求: 密码规则是 12位 数字 + 字母 混合后MD5加密 然后导出一个表格或者记录文件,文件没明确要求 实现过程: 1.MD5加密函数使用 SET @txt = '123456'; SELEC ...
- Arm V8 - ADRP指令
ADRP指令 作用 将当前指令所在页的基地址加/减去字节差,并写入目标寄存器 字节差:与目标地址页基地址的间隔字节数,其为PAGE_SIZE的整数倍 此时的字节差就是指令所操作的立即数 该指令通常配合 ...
- conda报错、anconda报错:requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
anconda报错,报错信息: requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 不能使用c ...