主要参考:

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 的代码

AddIdentityCore

IdentityServiceCollectionExtensions.cs

负责UserManger 之类的

AddDefaultUI

IdentityBuilderUIExtensions.cs

负责 Ui 的咚咚, SignInManager 也在这里

AddDefaultTokenProviders

IdentityBuilderExtensions.cs

Email, Phone reset password 这些 token 的做法

还有一个 AddIdentity

IdentityServiceCollectionExtensions.cs

这个是以前还没有 AddDefaultIdentity 的时候的调用, 如果现在没有要 DefaultUI 也是会用回 AddIdentity + AddDefaultTokenProviders

注: AddDefaultUI 会有 Error Page 哦, 可能破坏 router, 除非你真的要, 不然建议不适用它.

整个过程就是, 你要什么就拿什么来用, 或拿来改就是了, 最重要的是全部过一遍, 知道它都搞了些什么.

Identity – Introduction & Scaffold的更多相关文章

  1. 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 ...

  2. Discrete.Differential.Geometry-An.Applied.Introduction(sig2013) 笔记

    The author has a course on web: http://brickisland.net/DDGSpring2016/ It has more reading assignment ...

  3. A.Kaw矩阵代数初步学习笔记 1. Introduction

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  4. Security » Authentication » Identity介绍

    Introduction to Identity¶ By Pranav Rastogi, Rick Anderson, Tom Dykstra, Jon Galloway and Erik Reita ...

  5. [转]DCM Tutorial – An Introduction to Orientation Kinematics

    原地址http://www.starlino.com/dcm_tutorial.html Introduction This article is a continuation of my IMU G ...

  6. TIJ——Chapter One:Introduction to Objects

    ///:~容我对这个系列美其名曰"读书笔记",其实shi在练习英文哈:-) Introduction to Objects Object-oriented programming( ...

  7. 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 ...

  8. 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 ...

  9. Introduction to the POM

    原文:https://maven.apache.org/guides/introduction/introduction-to-the-pom.html Introduction to the POM ...

  10. asp.net core系列 46 Identity介绍

    一. Identity 介绍 ASP.NET Core Identity是一个会员系统,可为ASP.NET Core应用程序添加登录功能.可以使用SQL Server数据库配置身份以存储用户名,密码和 ...

随机推荐

  1. 可视化—gojs 超多超实用经验分享(二)

    想了想序号还是接上一篇分享的的序号接着写,如果在本文中没有获取需要的答案,可以移步去看看上一篇的分享.gojs 超多超实用经验分享(一) 目录 22. 指定线段连接到节点的某一个特定的接口上 23. ...

  2. oeasy教您玩转vim - 1 - # 存活下来 🥊

    存活下来 更新 apt 源,升级 vim vim 是什么 vim 是类 unix 系统上的一个文本编辑神器,在 Linux 系统环境中也被许多程序员使用,书写程序和文档. 我们本次课程将围绕 Vim ...

  3. oeasy教您玩转vim - 65 - # 批处理操作

    ​ 批处理操作 回忆上次 我们上次参数列表 arguments list 所谓参数列表指的是 vim 打开的 参数列表 参数会加载到内存中成为 buffer 参数的控制 :arga filename ...

  4. 全网最适合入门的面向对象编程教程:20 类和对象的 Python 实现-组合关系的实现与 CSV 文件保存

    全网最适合入门的面向对象编程教程:20 类和对象的 Python 实现-组合关系的实现与 CSV 文件保存 摘要: 本文主要介绍了在使用 Python 面向对象编程时,如何实现组合关系,同时对比了组合 ...

  5. 在.NET Web API设置响应输出Json数据格式常用的两种方式

    前言 在ASP.NET Core Web API中设置响应输出Json数据格式常用以下两种方式:可以通过添加System.Text.Json或Newtonsoft.JsonJSON序列化和反序列化库在 ...

  6. Jenkins 添加Linux固定代理节点

    实践环境 Jenkins 2.304 jdk-8u131-linux-x64.rpm centos-release-7-9.2009.1.el7.centos.x86_64 操作步骤 安装JDK 在预 ...

  7. .NET 8 通用权限框架 前后端分离,开箱即用

    前言​ 推荐一个基于.NET 8 实现的通用权限开发框架Admin.NET,前端使用Vue3/Element-plus开发. 基于.NET 8(Furion)/SqlSugar实现的通用管理平台.整合 ...

  8. 【Zookeeper】Re02 CuratorAPI

    Curator,提供给Java操作ZK的API组件: 需要的组件依赖: <!-- https://mvnrepository.com/artifact/org.apache.curator/cu ...

  9. 使用AI技术(单张图片或文字)生产3D模型 —— Ai生成3D模型的时代来了

    地址: https://www.bilibili.com/video/BV1A2421P7pH/ 视频用到的工具voxcraft体验地址:https://voxcraft.ai/

  10. 如何构建“集成神经网络”“Ensemble neural network”

    参考: https://arxiv.org/pdf/1603.05691.pdf 本文要讨论的是如何构建"集成神经网络"("Ensemble neural network ...