azure JWT授权问题ASP.NET Web API

ogq8wdun  于 2023-10-22  发布在  .NET
关注(0)|答案(1)|浏览(123)

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" 
},
2nbm6dog

2nbm6dog1#

对我来说,在startup.cs中更改JWT授权的配置,添加Default Schemas解决了404的问题。

builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
    var configuration = builder.Configuration;
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidAudience = configuration["AuthSettings:Audience"],
        ValidIssuer = configuration["AuthSettings:Issuer"],
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["AuthSettings:Key"])),
    };
});

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"));
});

相关问题