Hello,
recently i've upgraded my app to Asp.Net core 2.0 and i have strange behavior when using Jwt Bearer Authentication.
Only first request to the endpoint passes successfully, all other request get '401 Unauthorized' without any errors.
It looks like it can't get the token and build Claims for all other requests.
Interesting fact is App Insights trace shows that "Successfully validated the token", but afterwards challenge for bearer is sent again.
Maybe it's totally my bad, but i have no idea, what is wrong - in previous version everything was ok.
My startup file:
Startup.txt
My CsProj:
Version: preview2
Move UseAuthentication() ahead of UseMvc().
Unrelated: Remove o.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
Thank you very much, moving of UseAuthentication() upfront did the trick. This issue can be closed i guess.
@f1xxxer Is your Asp.Net Core 2 project a Web Api or a Web App? If its a Web Api, could you show me your repository? I'd want to implement token base authentication in a Asp.Net Core 2 Web Api project but unfortunately what I see in tutorials are for Asp.Net Core 1.1.
@lorenz31 yes, i have a web api project, can't show my repo though. Any way, assuming that you have updated everything to 2nd version, in StartUp.cs you need to configure your Jwt Authentication this way:
in ConfigureServices method
services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Audience = "your audience";
options.ClaimsIssuer = "claims issuer";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "claims issuer",
IssuerSigningKey = { your signing key }
};
});
in Configure
app.UseAuthentication();
app.UseMvc();
it is important that UseAuthentication() is before UseMvc()
https://github.com/aspnet/Announcements/issues/262 - here are some details, about Authentication changes
@f1xxxer can you help in this issue ? https://github.com/aspnet/Identity/issues/1422
@f1xxxer I understand. By the way, I already got it working. I just set aside using Asp.Net Identity for the built-in registration/login and just used the reference I found at code project.
@lorenz31 please share the CodeProject link.
Thanks
These sorts of issues belong in aspnet/security. Please ask questions about authentication there.
Most helpful comment
Version: preview2
Move UseAuthentication() ahead of UseMvc().
Unrelated: Remove
o.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;