Description
I have a client using Blazor Wasm, Blazor Client, preview 9, and I am having a issue with token authentication. If using Kestrel when a http request is made, I am getting 204 NoContent with a options verb, followed by a 500 server error, when using IIS, I am getting a browser erro, with advice about CORS, anyone can help me, my API is with authentication e authorization via JWT Token enabled and with CORS enabled too. The API works well with Postman, Fiddler, etc. but not with Blazor Client in browser.
In Startup I have
services.AddAuthentication().AddJwtBearer(cfg =>
{
cfg.RequireHttpsMetadata = false;
cfg.SaveToken = true;
cfg.TokenValidationParameters = new TokenValidationParameters()
{
ValidIssuer = "XXXX_XXXX_XXXX",
ValidAudience = "XXXX_XXXX_XXXX_XXXX",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))
};
});
services.AddAuthorization(auth =>
{
auth.AddPolicy("Bearer",
new
AuthorizationPolicyBuilder().AddAuthenticationSchemes(
JwtBearerDefaults.AuthenticationScheme).RequireAuthenticatedUser().Build());
});
...
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
...
app.UseCors("CorsPolicy");
When I remove the Bearer authorization from controller actions, the app request respond ok, with CORS working correct, I test with CORS and without CORS and this part works properly, but with authorization I have the before described behave, 204 options and 500 server error in test and CORS blocking in browser with the API hosted in IIS. What can I do? Anyone have some idea about this?