简体   繁体   English

IdentityServer4获取访问令牌

[英]IdentityServer4 get access token

Is it possible to get generated access token from IExtensionGrantValidator or ICustomTokenRequestValidator implementations or I must implement another validator? 是否可以从IExtensionGrantValidatorICustomTokenRequestValidator实现获取生成的访问令牌,或者我必须实现另一个验证程序? 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.

相关问题 如何获取访问令牌和用户信息:使用 AspNet Identity 的 IdentityServer4 - How to Get Access Token and User Info: IdentityServer4 With AspNet Identity 如何通过方法(而非api)通过identityserver4获取访问令牌? - how to get access token via identityserver4 via method (not api)? identityserver4 中的委托令牌 - Delegation token in identityserver4 身份令牌不调用 IdentityServer4 GetProfileDataAsync,只调用访问令牌 - IdentityServer4 GetProfileDataAsync is not called for Identity Token, only Access Token ASP.NET Identity(使用IdentityServer4)获取外部资源oauth访问令牌 - ASP.NET Identity (with IdentityServer4) get external resource oauth access token 如何使用IdentityServer4访问令牌和[Authorize]属性 - How to use IdentityServer4 Access Token and [Authorize] attribute 无法使用IdentityServer4验证API中的JWT或access_token - Unable to Validate JWT or access_token in API with IdentityServer4 为什么IdentityServer4生成的访问令牌总是无效? - Why is the access token generated by IdentityServer4 always invalid? 对 identityserver4 代码流访问令牌的受众声明是错误的 - Audience claim on identityserver4 code flow access token is wrong 密码流的 IdentityServer4 客户端不包括访问令牌中的 TestUser 声明 - IdentityServer4 client for Password Flow not including TestUser claims in access token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM