简体   繁体   English

如何在 C# 中检索 JWT 令牌?

[英]How do I retrieve the JWT token in C#?

I retrieve an id_token from AzureAD which I can see in inspect element.我从 AzureAD 中检索了一个id_token ,我可以在检查元素中看到它。

I am simply trying to retrieve the token in C# so I can decrypt it but I am unable to do so.我只是想在 C# 中检索令牌,这样我就可以解密它,但我无法这样做。

I have tried:我试过了:

var authHeader = Request.Headers["id_token"];
var authHeader = Request.Form["id_token"];

JwtSecurityToken token = new JwtSecurityToken(authHeader);

Both return null.两者都返回空值。

You can get the id token by override OnSecurityTokenReceived method.您可以通过覆盖OnSecurityTokenReceived方法获取 id 令牌。

Add custom OnSecurityTokenReceived to the Notifications .将自定义OnSecurityTokenReceived添加到Notifications

Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthenticationFailed = OnAuthenticationFailed,
                    SecurityTokenReceived= OnSecurityTokenReceived
                }

Then you can get id token like this然后你可以像这样获得id令牌

private Task OnSecurityTokenReceived(SecurityTokenReceivedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
        {
            var idtoken = context.ProtocolMessage.IdToken;
            return Task.FromResult(0);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM