I have a problem with issuing JWT token.
When I add [Authorize]
to the API endpoint in my controller, and I try to access it via Swagger or Postman, it returns with an error 404. (JWT is being send in headers).
JWT:
eyJhbGciOiJIUzI1NiJ9.eyJFbWFpbCI6IjIyMjJAR01BSUwuQ09NIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZWlkZW50aWZpZXIiOiI4NzY2OTY1ZS0wMDU1LTQ1NWMtYWFjOS1mZmI3MDdiOGExZjkiLCJVc2VySWQiOiI4NzY2OTY1ZS0wMDU1LTQ1NWMtYWFjOS1mZmI3MDdiOGExZjkiLCJleHAiOjE2OTk5NjQzNTksImlzcyI6Imh0dHBzOi8vZ3Jvb3Zlc3luYy5henVyZXdlYnNpdGVzLm5ldCIsImF1ZCI6Imh0dHBzOi8vZ3Jvb3Zlc3luYy5henVyZXdlYnNpdGVzLm5ldCJ9.zJxkLPBAH4TO7Vvl6NH1MtKEGK2axvuhqtYtzqm0V5A
The Web API is being hosted in Azure for students.Startup.cs
file:
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
var configuration = builder.Configuration;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = configuration["AuthSettings:Audience"],
ValidIssuer = configuration["AuthSettings:Issuer"],
RequireExpirationTime = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["AuthSettings:Key"])),
ValidateIssuerSigningKey = true
};
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("Admin", policy => policy.RequireRole("admin"));
options.AddPolicy("Artist", policy => policy.RequireRole("artist"));
options.AddPolicy("Studio", policy => policy.RequireRole("studio"));
options.AddPolicy("User", policy => policy.RequireRole("user"));
});
appsettings.json
:
"AuthSettings": {
"Key": "x",
"Audience": "https://groovesync.azurewebsites.net",
"Issuer": "https://groovesync.azurewebsites.net"
},
1条答案
按热度按时间2nbm6dog1#
对我来说,在startup.cs中更改JWT授权的配置,添加Default Schemas解决了404的问题。