[英]IdentityServer4 get access token
Is it possible to get generated access token from IExtensionGrantValidator
or ICustomTokenRequestValidator
implementations or I must implement another validator? 是否可以从
IExtensionGrantValidator
或ICustomTokenRequestValidator
实现获取生成的访问令牌,或者我必须实现另一个验证程序? I can't find any properties from which I can get access token. 我找不到可以从中获取访问令牌的任何属性。
Well, within the context of retrieving it initially from IdentityServer, the token is in the AccessToken
property of the TokenResponse
instance returned. 好吧,在最初从IdentityServer检索它的上下文中,令牌位于返回的
TokenResponse
实例的AccessToken
属性中。
From within an ASP.NET Core application authenticating via IdentityServer, the fact that it came from IdentityServer is inconsequential. 在通过IdentityServer进行身份验证的ASP.NET Core应用程序中,它来自IdentityServer的事实是无关紧要的。 Generically, to retrieve an access token, you'd use:
通常,要检索访问令牌,请使用:
using Microsoft.AspNetCore.Authentication;
...
var token = await HttpContext.GetTokenAsync("access_token");
Here is a workaround, deps: IdentityServer4.AccessToken Validation 2.7 + IdentityModel 3.10 这是一种解决方法,具体取决于:IdentityServer4.AccessToken验证2.7 + IdentityModel 3.10
services.AddScoped(provider =>
{
var httpService = provider.GetService<IHttpContextAccessor>();
var token = httpService.HttpContext.Request.Headers["Authorization"][0].Remove(0, "Bearer ".Length);
var userGuid = Guid.Parse(httpService.HttpContext.User.FindFirst("sub").Value);
// Use token
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.