IdentityServer4(客户端授权模式)
1.新建三个项目
IdentityServer:端口5000
IdentityAPI:端口5001
IdentityClient:
2.在IdentityServer项目中添加IdentityServer4的包:Install-Package IdentityServer4
添加一个类:
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("api", "myapi")//定义资源名称
};
} public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "client",//客户端获取token时指定的ClientId值
AllowedGrantTypes = GrantTypes.ClientCredentials,//授权模式 ClientSecrets =
{
new Secret("secret".Sha256())//客户端获取token时指定的Secret值
},
AllowedScopes = { "api" }//设置可访问的资源名称
}
};
}
然后在该项目的Startup中注入:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
//注入到容器中
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources())//加载配置信息
.AddInMemoryClients(Config.GetClients());
} public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseIdentityServer();//管道
}
}
然后你可以访问http://localhost:5000/.well-known/openid-configuration
3.在IdentityAPI项目中添加一个控制器:控制器头要添加[Authorize]
添加身份验证中间件:① 验证传入令牌以确保它来自可信发行者,② 令牌验证是有效的,用于在这个API
Microsoft.AspNetCore.Authentication.JwtBearer
在该项目的Startup文件中
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters(); services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options => //使用IdentityServer作为授权模式
{
options.Authority = "http://localhost:5000";//服务地址
options.RequireHttpsMetadata = false; options.ApiName = "api";//访问的资源名称
});
} public void Configure(IApplicationBuilder app)
{
app.UseAuthentication();
app.UseMvc();
}
}
4.IdentityClient项目中添加IdentityModel 库
IdentityModel 包含了一个用于发现端点的客户端库。这样一来你只需要知道 IdentityServer 的基础地址,实际的端点地址可以从元数据中读取。
private static async Task MainAsync()
{
var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
if (disco.IsError)
{
Console.WriteLine(disco.Error);
return;
} // request token
var tokenClient = new TokenClient(disco.TokenEndpoint, "client", "secret");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api"); if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
} Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n"); // call api
var client = new HttpClient();
client.SetBearerToken(tokenResponse.AccessToken); var response = await client.GetAsync("http://localhost:5001/Home");
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
else
{
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(JArray.Parse(content));
}
Console.Read();
}
客户端授权模式通常用于服务器到服务器通信
IdentityServer4(客户端授权模式)的更多相关文章
- IdentityServer4 (1) 客户端授权模式(Client Credentials)
写在前面 1.源码(.Net Core 2.2) git地址:https://github.com/yizhaoxian/CoreIdentityServer4Demo.git 2.相关章节 2.1. ...
- IdentityServer4[3]:使用客户端认证控制API访问(客户端授权模式)
使用客户端认证控制API访问(客户端授权模式) 场景描述 使用IdentityServer保护API的最基本场景. 我们定义一个API和要访问API的客户端.客户端从IdentityServer请求A ...
- IdentityServer(二)客户端授权模式
前言 客户端授权模,客户端直接向Identity Server申请token并访问资源.客户端授权模式比较适用于服务之间的通信. 搭建Identity服务 新建名为 IdentityServer 的W ...
- 认证授权:IdentityServer4 - 各种授权模式应用
前言: 前面介绍了IdentityServer4 的简单应用,本篇将继续讲解IdentityServer4 的各种授权模式使用示例 授权模式: 环境准备 a)调整项目结构如下: b)调整cz.Id ...
- IdentityServer4 自定义授权模式
IdentityServer4除了提供常规的几种授权模式外(AuthorizationCode.ClientCredentials.Password.RefreshToken.DeviceCode), ...
- IdentityServer4(7)- 使用客户端认证控制API访问(客户端授权模式)
一.前言 本文已更新到 .NET Core 2.2 本文包括后续的Demo都会放在github:https://github.com/stulzq/IdentityServer4.Samples (Q ...
- AspNetCore中的IdentityServer4客户端认证模式实现
1 AuthorizationServer using IdentityServer4; using IdentityServer4.Models; public class Startup { pu ...
- IdentityServer4 (3) 授权码模式(Authorization Code)
写在前面 1.源码(.Net Core 2.2) git地址:https://github.com/yizhaoxian/CoreIdentityServer4Demo.git 2.相关章节 2.1. ...
- (十)React Ant Design Pro + .Net5 WebApi:后端环境搭建-IdentityServer4(二)授权模式
一.前言 先交代一下整个Demo项目结构: 一个认证服务(端口5000)IdentityServer4.Authentication 五个授权模式(两个控制台程序,三个MVC项目端口5001)文件夹G ...
随机推荐
- java web项目改装exe安装版
https://blog.csdn.net/rico_zhou/article/details/79868129java简单程序打包成exe https://blog.csdn.net/rico_zh ...
- Android入门教程(四)
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 学习Android要掌握Android程序结构,和通信技术,和如 ...
- 《Java理解程序逻辑试题分析》
1.以下关于开发java程序的描述错误的是 (). (选择一项) A:开发Java程序的步骤包括:编写源程序.编译.运行 B:编写的Java源程序文件使用 java作为扩展名 C:Java源文件经编译 ...
- Python实现 "反转字符串中的元音字母" 的方法
#coding=utf- def reverseVowels(s): """ :type s: str :rtype: str """ sS ...
- Android中创建自定义控件
1.创建一个TitleLayout继承LinearLayout: //创建自定义控件 public class TitleLayout extends LinearLayout { private f ...
- rust变量与可变性
fn main() { //let x = 5; let mut x = 5; //通过const定义常量名称要大写,并且值不可更改 const Y:i32=6; println!("Y i ...
- 解决vs2010按ctrl+f5,调试窗口一闪而过的方法
vs2010调试按F5与按Ctrl+F5有什么区别 Ctrl F5测试运行后不自动推出控制台,直接按F5会自动退出去 解决vs2010按ctrl+f5,调试窗口一闪而过的方法 http://hi.ba ...
- 并发下sftp连接报错——com.jcraft.jsch.JSchException: connection is closed by foreign host
当对单接口极限测试时,随着并发量上升,接口稳定性出现不稳定的情况,排查后台日志,发现报错在该接口调用sftp上传时出现问题(确切的是在初始化连接时失败) 原因:系统SSH终端连接数配置过小,查看虚拟机 ...
- USB:USB通信中的端点(endpoint)和四种传输模式
USB的传输模式有4种,分别是控制传输(Control Transfer).中断传输(Interrupt Transfer).批量传输或叫块传输(Bulk Transfer).实时传输或叫同步传输(I ...
- word 转 pdf,c#代码
通过使用 C# 控制 office 软件 com 组件转 pdf 1 word 转 pdf 方案二:可以使用 netoffice 进行转换 参考文档:https://netoffice.io/docu ...