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_tokenexpires_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。的更多相关文章

  1. ASP.NET Core 2.1 JWT token (一) - 简书

    原文:ASP.NET Core 2.1 JWT token (一) - 简书 JwtBearer认证是一种标准的,通用的,无状态的,与语言无关的认证方式.Bearer验证属于HTTP协议标准验证. 如 ...

  2. 在ASP.NET Core中实现一个Token base的身份认证

    注:本文提到的代码示例下载地址> How to achieve a bearer token authentication and authorization in ASP.NET Core 在 ...

  3. [转]NET Core中实现一个Token base的身份认证

    本文转自:http://www.cnblogs.com/Leo_wl/p/6077203.html 注:本文提到的代码示例下载地址> How to achieve a bearer token ...

  4. NET Core中实现一个Token base的身份认证

    NET Core中实现一个Token base的身份认证 注:本文提到的代码示例下载地址> How to achieve a bearer token authentication and au ...

  5. ASP.NET Core 2.1 JWT Token 使用 (二) - 简书

    原文:ASP.NET Core 2.1 JWT Token 使用 (二) - 简书 接上文,https://www.jianshu.com/p/c5f9ea3b4b65 ASP.NET Core 2. ...

  6. Retrofit Token过期 重新请求Token再去请求接口

    需求是这样的:请求接口A -- 服务器返回数据Token过期或失效  -- 重新请求Token并设置 -- 再去请求接口A 刚解决了这个问题,趁热打铁,写个博客记录一下:这个Token是添加到请求头里 ...

  7. 简述C#中IO的应用 RabbitMQ安装笔记 一次线上问题引发的对于C#中相等判断的思考 ef和mysql使用(一) ASP.NET/MVC/Core的HTTP请求流程

    简述C#中IO的应用   在.NET Framework 中. System.IO 命名空间主要包含基于文件(和基于内存)的输入输出(I/O)服务的相关基础类库.和其他命名空间一样. System.I ...

  8. .Net Core 项目区域请求设置

    .net core 和asp.net MVC区域请求有个区别,这里重点记录一下 asp.net MVC 区域请求直接是/区域名称/控制名称/方法名称,其他不需要设置任何东西,而Core 项目这样请求路 ...

  9. Core篇——初探Core的Http请求管道&&Middleware

    目录: 1.Core 处理HTTP请求流程 2.中间件(Middleware)&&处理流程 3.创建自定义中间件&&模拟Core的请求管道 Core 处理HTTP请求流 ...

  10. .net core将URL请求格式化为XML或JSON(网站动态生成sitemap.xml)

    .net core将URL请求格式化为XML或JSON(网站动态生成sitemap.xml) 首先设置 Startup.cs 文件 配置 ConfigureServices services .Add ...

随机推荐

  1. @RequestBody注解转对象中驼峰格式的参数无法接收到数据的问题解决方法

    1.问题:驼峰格式的参数传递到后端,@RequestBody注解标注的实体对象参数没有接收到对应的数据 前端传参:执行结果:请求参数实体: import lombok.Data; /** * 请求参数 ...

  2. Java学习十七—反射机制:解锁代码的无限可能

    Java学习十七-反射机制:解锁代码的无限可能 一.关于反射 1.1 简介 Java 反射(Reflection)是Java 的特征之一,它允许程序在运行时动态地访问和操作类的信息,包括类的属性.方法 ...

  3. NOIP2024模拟3:一路破冰

    NOIP2024模拟3:一路破冰 雨后的青山.--240316 A-无向图删边 一句话题面:规定一轮中的删边方式为:按边权递减且每轮删掉的边集中没有环.问每条边会在第几轮被删除. 暴力的想法就是跑 \ ...

  4. 强行修改 User-Agent, 访问对应的端

    location /{ proxy_pass http://localhost:18080; proxy_set_header User-Agent "Mozilla/5.0 (Window ...

  5. 可爱的 Python: 创建声明性迷你语言

    编程为断言而不是指令 Python 的面向对象和透明自省功能使您可以轻松地创建用于编程任务的声明性迷你语言.在本专栏文章中,David 并未仔细研究如何使用 Python 来解释或翻译其它的专门语言( ...

  6. Windows下驱动安装

    推荐使用金山毒霸中的电脑医生进行驱动或DLL文件的扫描,查找并下载 dll文件下载地址:  https://www.wenjian.net/ 可以进行下载,告诉该文件的放置路径 其他:

  7. MySQL之sql_mode

    sql_mode是个很容易被忽视的变量,默认值是空值,在这种设置下是可以允许一些非法操作的,比如允许一些非法数据的插入.在生产环境必须将这个值设置为严格模式,所以开发.测试环境的数据库也必须要设置,这 ...

  8. springboot 前后端大打包成一个JAR

    1.概述 现在开发使用前后端开发机制,在部署的时候,我们需要将前后端分别打包,使用nginx 进行统一部署.这样就比较复杂,我们可以使用前后端打包到一个jar中,这样我们只需要一个包就可以了. 2.实 ...

  9. CDP与Selenium相结合——玩转网页端自动化数据采集/爬取程序

    Selenium Selenium 是一款开源且可移植的自动化软件测试工具,专门用于测试网页端应用程序或者采集网页端数据.它能够在不同的浏览器和操作系统上运行,具有很强的跨平台能力.Selenium可 ...

  10. uni-app项目uview的表单验证在小程序上不生效

    前情 uni-app是我比较喜欢的跨平台框架,它能开发小程序/H5/APP(安卓/iOS),重要的是对前端开发友好,自带的IDE让开发体验非常棒,公司项目就是主推uni-app,在uniapp生态中u ...