Identity Server 4 从入门到落地(十)—— 编写可配置的客户端和Web Api
前面的部分:
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的更多相关文章
- Identity Server 4 从入门到落地(八)—— .Net Framework 客户端
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(十二)—— 使用Nginx集成认证服务
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(十一)—— Docker部署
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(九)—— 客户端User和Role的解析
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(五)—— 使用Ajax访问Web Api
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(四)—— 创建Web Api
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(六)—— 简单的单页面客户端
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(七)—— 控制台客户端
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Identity Server 4 从入门到落地(一)—— 从IdentityServer4.Admin开始
最近项目中需要使用Identity Server 4,以前对这个技术只是有些了解,没有系统研究过,网上相关的资料不少,大多是从编写一个简单的认证服务开始,离能够落地使用有相当的距离,理论学习如何不结合 ...
随机推荐
- 暑假算法练习Day1
为了加强个人的算法能力,所以准备每天都做适当的算法练习,并在隔天加以回顾. 依托PTA.Leetcode平台进行训练(暂定每天三题C++,对于Leetcode平台上的练习,由于其解题需以类的形式进行提 ...
- 作为Bootstrap中文站维护者-我们再次翻译BootstrapVue项目
点击立即进入BootstrapVue中文站 http://code.z01.com/bootstrap-Vue Bootstrap-Vue 基于全球最流行的前端框架组合应用系统 项目介绍 Bootst ...
- Python进阶(多线程)
多线程结构 import threading def worker():#子线程要执行的具体逻辑代码函数 print('threading') t1 = threading.current_threa ...
- Linux常见目录结构
目录 描述 /home 包含Linux系统上各用户的主目录,子目录默认以该用户名命名 /etc 包含Linux系统上大部分的配置文件,建议修改配置文件之前先备份 /var 该目录存放不经常变化的数据, ...
- Highcharts › 自由绘图
... <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...
- tomcat更改端口号and设置cmd别名
1.修改端口号 打开tomcat的conf\server.xml 这个是项目访问的端口号了 这个也要注意更改一下,避免冲突 2.接下来要设置cmd别名 用文本编辑器打开bin\catalina.bat ...
- Redis | 第7章 Redis 服务器《Redis设计与实现》
目录 前言 1. 命令请求的执行过程 1.1 发送命令请求 1.2 读取命令请求 1.3 命令执行器(1):查找命令实现 1.4 命令执行器(2):执行预备操作 1.5 命令执行器(3):调用命令的实 ...
- Java培训机构如何选择才能避免被骗?
近年来,随着IT行业的快速崛起,各类互联网人才供不应求,而Java工程师作为目前最为火爆的岗位之一,更是以高薪+高新技术的标签受到了人们的广泛关注.许多年轻人也看到了这个行业的发展前景,决定报名培训机 ...
- 洛谷 P5469 - [NOI2019] 机器人(区间 dp+拉格朗日插值)
洛谷题面传送门 神仙题,放在 D1T2 可能略难了一点( 首先显然对于 P 型机器人而言,将它放在 \(i\) 之后它会走到左边第一个严格 \(>a_i\) 的位置,对于 Q 型机器人而言,将它 ...
- 选择省份、选择省市区举例【c#】【js】
<style type="text/css"> .labelhide { -webkit-box-shadow: 0px 1px 0px 0px #f3f3f3 !im ...