.net core 用 identitymodel 请求token。
identitymodel 也有在Nuget里搜索和安装。
identitymodel 扩展了HttpClient的一些方法用于token请求。
例如:client.RequestTokenAsync(new TokenRequest)
Token Endpoint
The client library for the token endpoint (OAuth 2.0 and OpenID Connect) is provided as a set of extension methods for HttpClient. This allows creating and managing the lifetime of the HttpClient the way you prefer - e.g. statically or via a factory like the Microsoft HttpClientFactory.
Requesting a token
The main extension method is called RequestTokenAsync - it has direct support for standard parameters like client ID/secret (or assertion) and grant type, but it also allows setting arbitrary other parameters via a dictionary. All other extensions methods ultimately call this method internally:
var client = new HttpClient(); var response = await client.RequestTokenAsync(new TokenRequest
{
Address = "https://demo.identityserver.io/connect/token",
GrantType = "custom", ClientId = "client",
ClientSecret = "secret", Parameters =
{
{ "custom_parameter", "custom value"},
{ "scope", "api1" }
}
});
The response is of type TokenResponse and has properties for the standard token response parameters like access_token, expires_in etc. You also have access to the the raw response as well as to a parsed JSON document (via the Raw and Json properties).
Before using the response, you should always check the IsError property to make sure the request was successful:
if (response.IsError) throw new Exception(response.Error); var token = response.AccessToken;
var custom = response.Json.TryGetString("custom_parameter");
Requesting a token using the client_credentials Grant Type
The RequestClientCredentialsToken extension method has convenience properties for the client_credentials grant type:
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = "https://demo.identityserver.io/connect/token", ClientId = "client",
ClientSecret = "secret",
Scope = "api1"
});
Requesting a token using the password Grant Type
The RequestPasswordToken extension method has convenience properties for the password grant type:
var response = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
{
Address = "https://demo.identityserver.io/connect/token", ClientId = "client",
ClientSecret = "secret",
Scope = "api1", UserName = "bob",
Password = "bob"
});
Requesting a token using the authorization_code Grant Type
The RequestAuthorizationCodeToken extension method has convenience properties for the authorization_code grant type and PKCE:
var response = await client.RequestAuthorizationCodeTokenAsync(new AuthorizationCodeTokenRequest
{
Address = IdentityServerPipeline.TokenEndpoint, ClientId = "client",
ClientSecret = "secret", Code = code,
RedirectUri = "https://app.com/callback", // optional PKCE parameter
CodeVerifier = "xyz"
});
Requesting a token using the refresh_token Grant Type
The RequestRefreshToken extension method has convenience properties for the refresh_token grant type:
var response = await _client.RequestRefreshTokenAsync(new RefreshTokenRequest
{
Address = TokenEndpoint, ClientId = "client",
ClientSecret = "secret", RefreshToken = "xyz"
});
Requesting a Device Token
The RequestDeviceToken extension method has convenience properties for the urn:ietf:params:oauth:grant-type:device_code grant type:
var response = await client.RequestDeviceTokenAsync(new DeviceTokenRequest
{
Address = disco.TokenEndpoint, ClientId = "device",
DeviceCode = authorizeResponse.DeviceCode
});
参考:https://identitymodel.readthedocs.io/en/latest/client/token.html
例子code:
using System;
using System.Net.Http;
using IdentityModel.Client; namespace ClientCredential
{
class Program
{
static void Main(string[] args)
{
try
{
new Program().GetAsync();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
} Console.ReadKey(); } public async void GetAsync()
{
var diso = await DiscoveryClient.GetAsync("http://localhost:5003");
if (diso.IsError)
{
System.Console.WriteLine("diso.Error");
}
var tokenClient = new TokenClient(diso.TokenEndpoint, "client", "secrt");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api");
if (tokenResponse.IsError)
{
System.Console.WriteLine(tokenResponse.Error);
}
else
{
System.Console.WriteLine(tokenResponse.Json);
} using (var httpClient = new HttpClient())
{
httpClient.SetBearerToken(tokenResponse.AccessToken);
var response = await httpClient.GetAsync("http://localhost:5001/api/values");
if (response.IsSuccessStatusCode)
{
System.Console.WriteLine(await response.Content.ReadAsStringAsync());
}
} }
}
}
.net core 用 identitymodel 请求token。的更多相关文章
- ASP.NET Core 2.1 JWT token (一) - 简书
原文:ASP.NET Core 2.1 JWT token (一) - 简书 JwtBearer认证是一种标准的,通用的,无状态的,与语言无关的认证方式.Bearer验证属于HTTP协议标准验证. 如 ...
- 在ASP.NET Core中实现一个Token base的身份认证
注:本文提到的代码示例下载地址> How to achieve a bearer token authentication and authorization in ASP.NET Core 在 ...
- [转]NET Core中实现一个Token base的身份认证
本文转自:http://www.cnblogs.com/Leo_wl/p/6077203.html 注:本文提到的代码示例下载地址> How to achieve a bearer token ...
- NET Core中实现一个Token base的身份认证
NET Core中实现一个Token base的身份认证 注:本文提到的代码示例下载地址> How to achieve a bearer token authentication and au ...
- ASP.NET Core 2.1 JWT Token 使用 (二) - 简书
原文:ASP.NET Core 2.1 JWT Token 使用 (二) - 简书 接上文,https://www.jianshu.com/p/c5f9ea3b4b65 ASP.NET Core 2. ...
- Retrofit Token过期 重新请求Token再去请求接口
需求是这样的:请求接口A -- 服务器返回数据Token过期或失效 -- 重新请求Token并设置 -- 再去请求接口A 刚解决了这个问题,趁热打铁,写个博客记录一下:这个Token是添加到请求头里 ...
- 简述C#中IO的应用 RabbitMQ安装笔记 一次线上问题引发的对于C#中相等判断的思考 ef和mysql使用(一) ASP.NET/MVC/Core的HTTP请求流程
简述C#中IO的应用 在.NET Framework 中. System.IO 命名空间主要包含基于文件(和基于内存)的输入输出(I/O)服务的相关基础类库.和其他命名空间一样. System.I ...
- .Net Core 项目区域请求设置
.net core 和asp.net MVC区域请求有个区别,这里重点记录一下 asp.net MVC 区域请求直接是/区域名称/控制名称/方法名称,其他不需要设置任何东西,而Core 项目这样请求路 ...
- Core篇——初探Core的Http请求管道&&Middleware
目录: 1.Core 处理HTTP请求流程 2.中间件(Middleware)&&处理流程 3.创建自定义中间件&&模拟Core的请求管道 Core 处理HTTP请求流 ...
- .net core将URL请求格式化为XML或JSON(网站动态生成sitemap.xml)
.net core将URL请求格式化为XML或JSON(网站动态生成sitemap.xml) 首先设置 Startup.cs 文件 配置 ConfigureServices services .Add ...
随机推荐
- Power BI新卡片更改显示单位
Power BI 不知道什么时候发布了新卡片,照现在官方来说,该视觉对象目前还属于预览版,但已经可以正常使用了,对比旧的卡片,显示效果个人觉得会友好一些,详见官方说明:创建"新"卡 ...
- Vue2.x 常用功能和方法
Vue 生命周期 beforeCreate (组件实例刚被创建,组件属性计算之前,如 data 属性等) created (组件实例创建完成, 属性已绑定,但 DOM 还未生成, $el 属性不存在) ...
- Postman 免登录测试后端接口
今天跟测试同学学习了下用Postman免登录测试后端接口,测试同学除了会对我们系统前端测试外,一些后端接口涉及危险操作也会使用Postman 对接口进行测试,这个时候就需要解决一个接口免登录的问题,他 ...
- orange pi 香橙派 zero 刷openwrt当作有wifi的小路由器用
前面写过我用香橙派zero来测量温度 https://www.cnblogs.com/jar/p/15848178.html 最近准备把他改造成路由器 https://www.right.com.cn ...
- 关于免费笔记软件Obsidian和免费同步,长文,保存观看
前言 这段可以略过 最早使用的笔记软件(应该说是网页摘录软件)是网文快捕 CyberArticle ,但不停换电脑后当年保存的资料基本都遗失了,那可是我翻阅众多涩涩网站的精华文章. 后来网文快捕推出了 ...
- Golang的GMP调度模型与源码解析
0.引言 我们知道,这当代操作系统中,多线程和多进程模型被广泛的使用以提高系统的并发效率.随着互联网不断的发展,面对如今的高并发场景,为每个任务都创建一个线程是不现实的,使用线程则需要系统不断的在用户 ...
- P5987 [PA2019] Terytoria / 2023NOIP A层联测13 T3 全球覆盖
P5987 [PA2019] Terytoria / 2023NOIP A层联测13 T3 全球覆盖 题面及数据范围 对于一个点对,可以降维为线段,转化为 1 维的问题. 如图: 我们可以在横着的方向 ...
- 高性能计算-雅可比算法MPI通信优化(5)
雅可比算法原理:如下图对方阵非边界元素求上下左右元素的均值,全部计算元素的数值计算完成后更新矩阵,进行下一次迭代. 测试目标:用MPI实现对8*8方阵雅可比算法迭代并行计算,用重复非阻塞的通信方式 # ...
- golang之go-spew
github: https://github.com/davecgh/go-spew 我们在使用Golang(Go语言)开发的过程中,会通过经常通过调试的方式查找问题的原因,解决问题,尤其是当遇到一个 ...
- MySQL命令行客户端工具之mycli
官网: mycli 目前市面上存在各种五花八门的图形界面客户端,如 phpmyadmin,navicat 以及官方的 MySQL Workbench 等等.而在日常工作或者使用中,通过命令连接 MyS ...