简体   繁体   中英

How do I retrieve the JWT token in C#?

I retrieve an id_token from AzureAD which I can see in inspect element.

I am simply trying to retrieve the token in C# so I can decrypt it but I am unable to do so.

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.

Add custom OnSecurityTokenReceived to the Notifications .

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

Then you can get id token like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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