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数据库配置身份以存储用户名,密码和 ...
随机推荐
- PHP中的__autoload()和spl_autoload_register()
php的__autoload函数是一个魔术函数,在这个函数出现之前,如果一个php文件里引用了100个对象,那么这个文件就需要使用include或require引进100个类文件,这将导致该php文件 ...
- oeasy教您玩转vim - 2 - # 使用帮助
回忆上节课内容 更新和运行 vim 进入和退出 vim 存活了下来 从中我们知道 vim 有两种模式:正常模式(Normal mode)和命令行模式 (Command-Line mode) 为了您能更 ...
- 毕设项目:springboot+vue实现的在线求职平台
一.前言 随着信息技术的飞速发展和互联网的普及,线上求职已成为众多求职者和企业招聘的重要渠道.为满足市场需求,我们利用Spring Boot和Vue技术栈,开发了一款功能全面.用户友好的在线求职平台. ...
- 单细胞测序最好的教程(十四)测序原始数据公开至NCBI数据库
作者按 国内对于单细胞测序相关的中文教程确实不够全面,当然NCBI官网给的上传教程也比较详细了,所以变成了会者不难.本教程你现在可能用不上,但是你如果做单细胞测序,那么未来你一定会用上,建议收藏. 在 ...
- 2023/4/17 SCRUM个人博客
1.我昨天的任务 学习了easydict库的基本操作 2.遇到了什么困难 没有找到合适的人脸识别库 3.我今天的任务 初步学习dlib的安装,了解dlib的基础组件
- 在 Hub 上使用 Presidio 进行自动 PII 检测实验
我们在 Hugging Face Hub 上托管的机器学习 (ML) 数据集中发现了一个引人关注的现象: 包含个人未经记录的私密信息.这一现象为机器学习从业者带来了一些特殊挑战. 在本篇博客中,我们将 ...
- 【Scala】07 集合
分三大类: 序列 Seq 集 Set 映射 Map 所有集合类型都扩展自Iterable特质(可迭代的) 所有集合类型都提供[可变]和[不可变]的版本 归纳在下面两个包中 scala.collecti ...
- 【Server - 运维】更改腾讯云数据库参数设置
购买的MySQL实例是一个屏蔽了后台设置的服务器: 默认大小写设置是使用严格区分的: 要设置忽略大小写,就要在my.cnf中更改配置参数 https://cloud.tencent.com/devel ...
- How to evaluate the Messi Hong Kong fraud incident?
Who is Lionel Messi? URL: https://en.wikipedia.org/wiki/Lionel_Messi As a famous football player, Me ...
- 【转载】 SLI导致双显卡被TensorFlow同时占用问题(Windows下) ---------- (windows环境下如何为tensorflow安装多个独立的消费级显卡)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_21368481/article/d ...