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数据库配置身份以存储用户名,密码和 ...
随机推荐
- 图扑低代码数字孪生 Web SCADA 智慧钢厂
2024 年 4 月,中国钢铁工业协会发布了<钢铁行业数字化转型评估报告(2023年)>(以下简称<报告>).<报告>指出,绝大部分钢铁企业建立了数字化转型相关管理 ...
- java spring boot 2 开发实战 mybtis 基础部份从搭建到第一个完整测试(从环境到测试用例二部份)
本案例是java sping boot 2.2.1 mybtis 基础部份 第一步搭建环境:安装依赖 由于我们公司项目是1.8 环境不能乱,我现在自己的电脑是1.8环境,所以本次整理的boot 代 ...
- 《Programming from the Ground Up》阅读笔记:p88-p94
<Programming from the Ground Up>学习第5天,p88-p94总结,总计7页. 一.技术总结 1.touppercase.s #PURPOSE: This pr ...
- 文件系统(十一):Linux Squashfs只读文件系统介绍
liwen01 2024.07.21 前言 嵌入式Linux系统中,squashfs文件系统使用非常广泛.它主要的特性是只读,文件压缩比例高.对于flash空间紧张的系统,可以将一些不需要修改的资源打 ...
- docker 容器卷
创建各种卷 [root@docker ~]# docker volume create mqy-vo101 mqy-vo101 [root@docker ~]# docker inspect mqy- ...
- web3产品介绍:mask将Web3的隐私和优势引入像Facebook和Twitter这样的社交媒体平台
介绍: Mask Network是一个开源的浏览器扩展,将Web3的隐私和优势引入像Facebook和Twitter这样的社交媒体平台.它是一个功能强大的工具,允许用户在社交媒体上享受区块链的隐私保护 ...
- 论文写作:test 和 testing 使用的区别
"test" 和 "testing" 的区别主要在于它们在句子中的用途和语法功能: Test: 名词: 指的是一次测试或考试.例如: "The stu ...
- 【转载】 介绍具有代表性的CPG控制机器人
原文地址: https://www.cnblogs.com/zhaochenliang/p/10453255.html ---------------------------------------- ...
- anaconda运行install命令报错:Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)'
运行命令: conda install mpi4py 报错: Retrieving notices: ...working... ERROR conda.notices.fetch:get_chann ...
- git 如何处理合并时存在的子模块冲突
如果另一个分支的子模块不同于当前分支,那么在拉取下来时,并不会更新本地子模块的版本,而会出现一个.diff文件,表示差异性.那么在合并代码时,可能会因为这个.dff文件冲突无法解决.产生这个问题的原因 ...