[Bug] HttpContext.GetTokenAsync("access_token") returns always null when using EnableTokenAcquisitionToCallDownstreamApi()
See original GitHub issueWhich version of Microsoft Identity Web are you using? Microsoft.Identity.Web 1.5.1
Where is the issue?
- Web app
- Sign-in users
- Sign-in users and call web APIs
- Web API
- Protected web APIs (validating tokens)
- Protected web APIs (validating scopes)
- Protected web APIs call downstream web APIs
- Token cache serialization
- In-memory caches
- Session caches
- Distributed caches
- Other (please describe)
Is this a new or an existing app? This is a new app
Repro
public void ConfigureServices(IServiceCollection services)
{
// Adds Microsoft Identity platform (AAD v2.0) support to protect this API
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(Configuration)
.EnableTokenAcquisitionToCallDownstreamApi()
.AddInMemoryTokenCaches();
// ...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
app.Use(async (context, next) =>
{
var token = await context.GetTokenAsync("access_token");
// token is always null here
await next();
});
// ...
}
Expected behavior
When JwtBearerOptions.SaveToken is true, context.GetTokenAsync("access_token")
returns the access token used in the request.
Actual behavior
context.GetTokenAsync("access_token")
returns always null when EnableTokenAcquisitionToCallDownstreamApi()
is used.
When I remove EnableTokenAcquisitionToCallDownstreamApi()
, I get the access token as expected.
Possible solution
I debugged the code, and the problem is in MicrosoftIdentityWebApiAuthenticationBuilder.cs
:
options.Events.OnTokenValidated = async context =>
{
await onTokenValidatedHandler(context).ConfigureAwait(false);
context.HttpContext.StoreTokenUsedToCallWebAPI(context.SecurityToken as JwtSecurityToken);
context.Success();
};
I don’t know why context.Success();
it’s needed, but when I remove it, everything works as expected.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (1 by maintainers)
Top Results From Across the Web
GetUserAccessTokenAsync() always returns null in ...
I am trying to renew the token with the below snippet. But somehow the result of await ctx.HttpContext.GetUserAccessTokenAsync(); is always null ...
Read more >Token is coming as null from GetTokenAsync in Blazor ...
I have created a blazor server app(.NET6) and I have used JWT authentication to authenticate the app with referring this Microsoft document ...
Read more >HttpContext.GetTokenAsync("access_token") is null
I'm using the sample code from the 'ASP.NET Core Storing Tokens' quickstart. I can login successfully, but when I interrogate the ...
Read more >AzureAD, Client confidential app calling webapi with a ...
I'm trying to develop an API which can be called from different web apps (both in client confidential or with the user token)....
Read more >.NET | Software Engineering
The user access token is saved to the HttpContext after a successful sign-in and the GetTokenAsync method with the “access_token” parameter is ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Remove it.
Success()
means you’ve taken care of everything and want to disable any further processing. In this case the only further processing you’re skipping is the part where we save the access token for later use. https://github.com/dotnet/aspnetcore/blob/61fc66cada5d72e76513918aee515846f2923b4e/src/Security/Authentication/JwtBearer/src/JwtBearerHandler.cs#L155-L161Included in 1.16 release