I have my regular web application that is signing in a user. Next the user may request my api for some data. Unfortunately Auth0 issues malformed access token and WebApi is not able to validate it - it thinks that is encrypted.
My code is very simple - this is how I configure regular web app:
builder.Services.AddAuth0WebAppAuthentication(o =>
{
builder.Configuration.GetSection("Auth0").Bind(o);
o.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.CallbackPath = new PathString("/auth-callback");
o.ResponseType = OpenIdConnectResponseType.Code;
o.Scope = "openid profile email offline_access";
o.SkipCookieMiddleware = false;
});
My appsettings are:
"Auth0": {
"ClientId": "<client id>",
"ClientSecret": "<client secret>",
"Domain": "<my domain>.eu.auth0.com"
}
Next, my configuration for WebApi is also simple:
builder.Services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o =>
{
o.Authority = builder.Configuration["Authentication:Authority"];
o.Audience = builder.Configuration["Authentication:Audience"];
});
And my appsettings:
"Authentication": {
"Authority": "https://<my domain>.eu.auth0.com/",
"Audience": "api://weather"
}
After some reading I tried to add “audience” to my regular web app, but there is no way to do it in Auth0 configuration. So I tried like this:
builder.Services.AddAuth0WebAppAuthentication(o =>
{
builder.Configuration.GetSection("Auth0").Bind(o);
o.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.CallbackPath = new PathString("/auth-callback");
o.ResponseType = OpenIdConnectResponseType.Code;
o.Scope = "openid profile email offline_access";
o.SkipCookieMiddleware = false;
o.LoginParameters = new Dictionary<string, string>
{
{ "audience", "api://weather" }
};
});
But this also does not work.
Issued tokens are malformed. Even jwt.io cannot parse them.
My Auth0 configuration is default.
Especially token settings for API application:
as you can see - encryption is turned off.
So what is the problem?