学习笔记之IdentityServer4(一)
快速入门IdentityServer4
概述
- 将 IdentityServer 添加到 ASP.NET Core 应用程序
- 配置 IdentityServer
- 为各种客户发行代币
- 保护 Web 应用程序和 API
- 添加对基于 EntityFramework 的配置的支持
- 添加对 ASP.NET 标识的支持
- 准备
- 安装模板
控制台窗口输入该命令,下载 IdentityServer 模板
dotnet new -i IdentityServer4.Templates //
- 创建项目
打开控制台窗口,请在控制台窗口键入以下命令
md quickstart //创建文件夹,文件夹名称quickstart
cd quickstart //进入文件夹quickstart md src //创建文件夹,文件夹名称src
cd src //进入文件夹src dotnet new is4empty -n IdentityServer //创建空项目,项目名称IdentityServer
命令执行完成后,将会创建以下文件
IdentityServer.csproj- 项目文件和一个Properties\launchSettings.json文件
Program.cs和Startup.cs- 主应用程序入口点
Config.cs - IdentityServer 资源和客户端配置文件
如果需要使用Visual Studio作为开发工具,我们还需要创建解决方案文件,命令如下:
dotnet new sln -n Quickstart
我们需要将文件添加至解决方案,命令如下:(请注意路径是否正确)
dotnet sln add .\src\IdentityServer\IdentityServer.csproj
- 开始学习
项目创建成功之后,将会为我们创建config 文件,Config文件的代码如下:

1 using IdentityServer4.Models;
2 using System.Collections.Generic;
3
4 namespace IdentityServer
5 {
6 public static class Config
7 {
8 public static IEnumerable<IdentityResource> IdentityResources =>
9 new IdentityResource[]
10 {
11 new IdentityResources.OpenId()
12 };
13
14 public static IEnumerable<ApiScope> ApiScopes =>
15 new ApiScope[]
16 { };
17
18 public static IEnumerable<Client> Clients =>
19 new Client[]
20 { };
21 }
22 }
- 自定义API范围
1 public static IEnumerable<ApiScope> ApiScopes =>
2 new List<ApiScope>
3 {
4 new ApiScope("api1", "MyApiName")
5 };
//ApiScope构造函数提供两个参数,第一个参数为name,第二个参数为displayname
如果您在生产环境种使用自定义API范围,您为您的API提供一个逻辑名称很重要,开发人员将使用它通过您的身份服务器请求您的API,它应该用简单的术语向开发人员描述您的api,displayname为用户提供apiScope的描述名称
- 定义客户端
我们将要定义一个客户端应用程序,我们将用它来访问我们的API,对于这种情况,客户端没有交互式用户,并将使用IdentityServer使用客户端密钥进行身份验证。为此我们需要添加一个客户端
1 public static IEnumerable<Client> Clients =>
2 new[]
3 {
4 new Client
5 {
6 ClientId = "client",
7
8 AllowedGrantTypes=GrantTypes.ClientCredentials,
9
10 ClientSecrets =
11 {
12 new Secret("secret","sha256")
13 },
14 AllowedScopes = { "api1" }
15 }
16 };
您可以将 ClientId 和 ClientSecret 视为应用程序本身的登录名和密码。它向身份服务器标识您的应用程序,以便它知道哪个应用程序正在尝试连接到它。
- 配置身份服务器
在Startup.cs文件中的ConfigureServices方法种定义资源和客户端,代码如下:
1 public void ConfigureServices(IServiceCollection services)
2 {
3 // uncomment, if you want to add an MVC-based UI
4 //services.AddControllersWithViews();
5
6 var builder = services.AddIdentityServer(options =>
7 {
8 // see https://identityserver4.readthedocs.io/en/latest/topics/resources.html
9 options.EmitStaticAudienceClaim = true;
10 })
11 .AddInMemoryIdentityResources(Config.IdentityResources)
12 .AddInMemoryApiScopes(Config.ApiScopes)
13 .AddInMemoryClients(Config.Clients);
14
15 // not recommended for production - you need to store your key material somewhere secure
16 builder.AddDeveloperSigningCredential();
17 }
配置您的身份服务器之后,访问路径https://localhost:5001/.well-known/openid-configuration,我们将会看到发现文档,发现文档是身份服务器种的标准节点,您的客户端和API将使用该文档来查看必要的配置数据,节点文档如下:
1 {
2 "issuer":"https://localhost:5001",
3 "jwks_uri":"https://localhost:5001/.well-known/openid-configuration/jwks",
4 "authorization_endpoint":"https://localhost:5001/connect/authorize",
5 "token_endpoint":"https://localhost:5001/connect/token",
6 "userinfo_endpoint":"https://localhost:5001/connect/userinfo",
7 "end_session_endpoint":"https://localhost:5001/connect/endsession",
8 "check_session_iframe":"https://localhost:5001/connect/checksession",
9 "revocation_endpoint":"https://localhost:5001/connect/revocation",
10 "introspection_endpoint":"https://localhost:5001/connect/introspect",
11 "device_authorization_endpoint":"https://localhost:5001/connect/deviceauthorization",
12 "frontchannel_logout_supported":true,
13 "frontchannel_logout_session_supported":true,
14 "backchannel_logout_supported":true,
15 "backchannel_logout_session_supported":true,
16 "scopes_supported":[
17 "openid",
18 "api1",
19 "offline_access"
20 ],
21 "claims_supported":[
22 "sub"
23 ],
24 "grant_types_supported":[
25 "authorization_code",
26 "client_credentials",
27 "refresh_token",
28 "implicit",
29 "urn:ietf:params:oauth:grant-type:device_code"
30 ],
31 "response_types_supported":[
32 "code",
33 "token",
34 "id_token",
35 "id_token token",
36 "code id_token",
37 "code token",
38 "code id_token token"
39 ],
40 "response_modes_supported":[
41 "form_post",
42 "query",
43 "fragment"
44 ],
45 "token_endpoint_auth_methods_supported":[
46 "client_secret_basic",
47 "client_secret_post"
48 ],
49 "id_token_signing_alg_values_supported":[
50 "RS256"
51 ],
52 "subject_types_supported":[
53 "public"
54 ],
55 "code_challenge_methods_supported":[
56 "plain",
57 "S256"
58 ],
59 "request_parameter_supported":true
60 }
在第一次启动时,IdentityServer 会为你创建一个开发者签名密钥,它是一个名为tempkey.jwk. 您不必将该文件签入您的源代码管理,如果它不存在,它将被重新创建
- 添加API测试
创建API项目,命令如下:
dotnet new webapi -n Api
将API项目添加至解决方案
dotnet sln add .\src\Api\Api.csproj
修改API项目的应用配置,修改运行端口号,我们可以通过编辑Properties 文件夹中的launchSettings.json文件来执行此操作,将应用程序的URL设置更改为:
"applicationUrl": "https://localhost:6001"
- 添加控制器
我们新建一个类,为IdentityController,此测试类将用于测试授权要求,以及通过API可视化身份识别
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Threading.Tasks;
5 using Microsoft.AspNetCore.Authorization;
6 using Microsoft.AspNetCore.Mvc;
7
8 namespace Api.Controllers
9 {
10 [Route("identity")]
11 [Authorize]
12 public class IdentityController: ControllerBase
13 {
14 public IActionResult Get()
15 {
16 return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
17 }
18 }
19 }
添加NuGet包依赖项目:Microsoft.AspNetCore.Authentication.JwtBearer,我们可以手动添加,也可以执行如下命令:
dotnet add .\\src\\api\\Api.csproj package Microsoft.AspNetCore.Authentication.JwtBearer
- 配置
最后一步是将身份验证服务添加到DI(依赖注入) 并将身份验证中间件添加到管道中,这些操作将:
- 验证传入的令牌以确保它来自受信任者的发行者
- 验证令牌是否有效与此API一起使用
我们查看以下API中的Startup代码,我们将其更新如下:

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Threading.Tasks;
5 using Microsoft.AspNetCore.Builder;
6 using Microsoft.AspNetCore.Hosting;
7 using Microsoft.AspNetCore.HttpsPolicy;
8 using Microsoft.AspNetCore.Mvc;
9 using Microsoft.Extensions.Configuration;
10 using Microsoft.Extensions.DependencyInjection;
11 using Microsoft.Extensions.Hosting;
12 using Microsoft.Extensions.Logging;
13 using Microsoft.IdentityModel.Tokens;
14 using Microsoft.OpenApi.Models;
15
16 namespace Api
17 {
18 public class Startup
19 {
20 public Startup(IConfiguration configuration)
21 {
22 Configuration = configuration;
23 }
24
25 public IConfiguration Configuration { get; }
26
27 // This method gets called by the runtime. Use this method to add services to the container.
28 public void ConfigureServices(IServiceCollection services)
29 {
30
31 services.AddControllers();
32 services.AddSwaggerGen(c =>
33 {
34 c.SwaggerDoc("v1", new OpenApiInfo { Title = "Api", Version = "v1" });
35 });
36
37 services.AddAuthentication("Bearer").AddJwtBearer("Bearer", options =>
38 {
39 options.Authority = "https://localhost:5001";
40
41 options.TokenValidationParameters = new TokenValidationParameters
42 {
43 ValidateAudience = false
44 };
45 });
46
47 }
48
49 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
50 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
51 {
52 if (env.IsDevelopment())
53 {
54 app.UseDeveloperExceptionPage();
55 app.UseSwagger();
56 app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Api v1"));
57 }
58
59 app.UseHttpsRedirection();
60
61 app.UseRouting();
62
63 app.UseAuthentication();
64 app.UseAuthorization();
65
66 app.UseEndpoints(endpoints =>
67 {
68 endpoints.MapControllers();
69 });
70
71 }
72 }
73 }
AddAuthentication将身份验证服务添加到 DI 并配置Bearer为默认方案。
UseAuthentication 将身份验证中间件添加到管道中,因此每次调用主机时都会自动执行身份验证。
UseAuthorization 添加授权中间件以确保匿名客户端无法访问我们的 API 端点。
https://localhost:6001/identity在浏览器上导航到控制器应返回 401 状态代码。这意味着您的 API 需要凭证并且现在受 IdentityServer 保护。
学习笔记之IdentityServer4(一)的更多相关文章
- OAuth 2.0学习笔记
文章目录 OAuth的作用就是让"客户端"安全可控地获取"用户"的授权,与"服务商提供商"进行互动. OAuth在"客户端&quo ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- seaJs学习笔记2 – seaJs组建库的使用
原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...
- CSS学习笔记
CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...
- HTML学习笔记
HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...
随机推荐
- Qt-可编辑的ListView
新建一个QML项目, main.cpp不动如下: #include <QGuiApplication> #include <QQmlApplicationEngine> int ...
- “入职一年,那个被高薪挖来的Android开发被劝退了。”
其实,在很多小伙伴的想法中,是希望通过跳槽实现薪酬涨幅,可是跳槽不是冲动后决定,应该谨慎啊~ 01 我的学弟,最近向我吐槽,2020 年上半年入职一家公司,当时是高薪挖走的他,所谓钱到位,工作也是充满 ...
- Docker部署Sql Server 2019实践
1. 拉取SqlServer2019镜像 sudo docker pull mcr.microsoft.com/mssql/server:2019-latest 2. 创建容器+挂载: sudo do ...
- MySQL-18-MHA+Atlas读写分离架构
Atlas介绍 Atlas是由Qihoo 360 Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目 它是在mysql-proxy 0.8.2版本的基础上,对其进行了优化,增加了 ...
- Python语言系列-08-面向对象3
反射 #!/usr/bin/env python3 # author: Alnk(李成果) # 反射 # hasattr getattr setattr delattr class Animal(ob ...
- 【笔记】KNN之分类准确度
分类准确度 分类准确度 以sklearn中的手写数字datasets.load_digits为例,其是8*8的图形,具有64个特征值,类别由0到9 我们挑选出666这个图形,将其可视化 X = dig ...
- SpringBoot开发二十二-统一处理异常
需求介绍 首先服务端分为三层:表现层,业务层,数据层. 请求过来先到表现层,表现层调用业务层,然后业务层调用数据层. 那么数据层出现异常它会抛出异常,那异常肯定是抛给调用者也就是业务层,那么业务层会再 ...
- JavaSE-基础语法
注释 单行注释: //注释 多行注释: /*注释*/ 文档注释: /** 文档注释 */ 标识符与关键字 下图为Java中所有的关键字 所有标识符必须以大小写字母或$或_开头,首字母之后可以用数字 不 ...
- 教你使用ApiPost中的全局参数和目录参数
前面的示例中,我们都是在单一接口中填入不同的请求header.query.body参数.但在实际项目中,对于一批接口,往往具有相同的请求参数.此时,我们可以利用全局参数或者目录参数实现. 例如:常见的 ...
- 单例对象 (Singleton)设计模式
单例的目的是为了保证运行时Singleton类只有唯一的一个实例,用于一些较大开销的操作. 饿汉式(没有线程安全问题): ' 由于使用static关键字进行了修饰,只能获取到一个对象,从而达到了单例, ...