前面的部分:

Identity Server 4 从入门到落地(一)—— 从IdentityServer4.Admin开始

Identity Server 4 从入门到落地(二)—— 理解授权码模式

Identity Server 4 从入门到落地(三)—— 创建Web客户端

Identity Server 4 从入门到落地(四)—— 创建Web Api

Identity Server 4 从入门到落地(五)—— 使用Ajax 访问 Web Api

Identity Server 4 从入门到落地(六)—— 简单的单页面客户端

Identity Server 4 从入门到落地(七)—— 控制台客户端

Identity Server 4 从入门到落地(八)—— .Net Framework 客户端

Identity Server 4 从入门到落地(九)—— 客户端User和Role的解析

认证服务和管理的github地址: https://github.com/zhenl/IDS4Admin

客户端及web api示例代码的github地址:https://github.com/zhenl/IDS4ClientDemo

前面的客户端和Web Api编写时,认证服务的地址等配置数据是在代码里写死的,在实际项目中这样肯定是不行的:我们不能因为认证服务的地址修改就重新修改和部署客户端和Web Api。另外在试验中我们也发现了很多不方便的地方,比如,每增加一类客户端,我们就需要修改Web Api,增加CORS的地址。因此,我们需要将这些配置数据转移到配置文件中进行维护。为此,我写了一个简单的扩展来帮助解决这个问题,项目代码地址:https://github.com/zhenl/ZL.IdentityServer4ClientConfig

使用这个扩展很简单,首先在需要创建Identit Server 4客户端的项目中引入包ZL.IdentityServer4ClientConfig :



然后在Program.cs中增加:

builder.Services.AddIS4OpenIdConnect(builder.Configuration); //Added

这样就可以了,完整的代码如下:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews(); builder.Services.AddIS4OpenIdConnect(builder.Configuration); //Added var app = builder.Build(); // Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles(); app.UseRouting();
app.UseAuthentication(); //增加的代码
app.UseAuthorization(); app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}")
.RequireAuthorization(); //Added; app.Run();

所有的配置项转移到appsettings.json中:

  "IdentityServer4Client": {
"Authority": "http://localhost:4010",
"ClientId": "myclient",
"ClientSecret": "secret",
"ResponseType": "code",
"SaveTokens": "true",
"RequireHttpsMetadata": "false",
"Scopes": [ "openid", "profile", "myapi" ],
"JsonKeys": [
{
"ClaimType": "age"
},
{
"ClaimType": "nickname",
"Key": "nickname"
},
{
"ClaimType": "mydefine",
"Key": "mydefine"
}
]
}

Web Api的扩展使用类似,也是先引入程序包,然后修改代码:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddIdentityServer4Api(builder.Configuration);//增加代码 builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); var app = builder.Build();
app.UseCors("cors");//增加代码 // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
} app.UseAuthentication();
app.UseAuthorization(); //增加代码
app.MapControllers()
.RequireAuthorization("ApiScope");//增加代码
; app.Run();

Web Api在appsettings.json中的配置项如下:

  "IdentityServer4Api": {
"Authority": "http://localhost:4010",
"CorsOrgins": [
"https://localhost:7002"
],
"Policies": [
{
"Name": "ApiScope",
"RequireAuthenticatedUser": "true",
"Claims": [
{
"ClaimType": "scope",
"AllowValues": [ "myapi" ]
}
]
}
],
"RequireHttpsMetadata": "false"
}

Identity Server 4 从入门到落地(十)—— 编写可配置的客户端和Web Api的更多相关文章

  1. Identity Server 4 从入门到落地(八)—— .Net Framework 客户端

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  2. Identity Server 4 从入门到落地(十二)—— 使用Nginx集成认证服务

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  3. Identity Server 4 从入门到落地(十一)—— Docker部署

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  4. Identity Server 4 从入门到落地(九)—— 客户端User和Role的解析

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  5. Identity Server 4 从入门到落地(五)—— 使用Ajax访问Web Api

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  6. Identity Server 4 从入门到落地(四)—— 创建Web Api

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  7. Identity Server 4 从入门到落地(六)—— 简单的单页面客户端

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  8. Identity Server 4 从入门到落地(七)—— 控制台客户端

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  9. Identity Server 4 从入门到落地(一)—— 从IdentityServer4.Admin开始

    最近项目中需要使用Identity Server 4,以前对这个技术只是有些了解,没有系统研究过,网上相关的资料不少,大多是从编写一个简单的认证服务开始,离能够落地使用有相当的距离,理论学习如何不结合 ...

随机推荐

  1. 问题 D: 某种序列

    题目描述 数列A满足An = An-1 + An-2 + An-3, n >= 3  编写程序,给定A0, A1 和 A2, 计算A99 输入 输入包含多行数据  每行数据包含3个整数A0, A ...

  2. Vue组件传值prop验证方式

    在Vue组件开发过程中,父组件要经常给子组件传递数据,在传递数据的过程中,子组件可以使用prop来接收父组件传递的值,同时呢,我们可以为组件的 prop 指定验证要求,例如你知道的这些类型.如果有一个 ...

  3. Nginx通过ngx_http_limit_req_module实现限制请求数、限速、白名单

    /etc/nginx/limit/white_list:白名单,key-value形式,支持掩码网段 #test 192.168.50.42 0; 192.168.50.0/24 0; /etc/ng ...

  4. PAT A1091——BFS

    Acute Stroke One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. ...

  5. [bzoj2245]工作安排

    由于价格是单调递增的,因此可以直接建立Si条边,流量和价格依次为给定的函数即可(如果价格不递增,可以考虑动态加边等操作) 1 #include<bits/stdc++.h> 2 using ...

  6. 【Golang】基于beego/orm实现相同表结构不同表名的分表方法实现

    一.背景 在业务场景开发的过程中, 随着数据量的增加,相同表结构不同表名的分表策略是常用的方案选择之一.如下以golang做为后端业务开发,尝试修改beego的orm库做一个相同表结构不同表名的分表实 ...

  7. vue 3 学习笔记 (七)——vue3 中 computed 新用法

    vue3 中 的 computed 的使用,由于 vue3 兼容 vue2 的选项式API,所以可以直接使用 vue2的写法,这篇文章主要介绍 vue3 中 computed 的新用法,对比 vue2 ...

  8. Sentry 监控 - Snuba 数据中台架构(SnQL 查询语言简介)

    本文描述了 Snuba 查询语言 (SnQL). 系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒 ...

  9. 使用protobuf-java-format包 JsonFormat转Json部分默认值字段消失问题

    使用protobuf-java-format包 JsonFormat转Json部分默认值字段消失问题 1.产生的bug XXXXXXXXRequest.Builder request = XXXXXX ...

  10. Docker Hadoop 配置常见错误及解决办法

    Docker Hadoop 配置常见错误及解决办法 问题1:wordcount运行卡住,hadoop 任务运行到running job就卡住了 INFO mapreduce.Job: Running ...