【发布时间】:2021-05-18 09:15:03
【问题描述】:
我有一个非常有趣的问题。我有一个 .Net Core 3.1 Rest Web API。背景资料:
- 我已根据我们的其他微服务正确配置了身份验证。
- POST 正确验证并返回 200 OK
- PUT 操作返回 401 UnAuthorized
- 我在逐个路由的身份验证方面什么都不做
控制器sn-p:
[Route("flights")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class FlightsController : BaseController
{
private readonly ILogger<FlightsController> _logger;
private readonly IStringLocalizer _stringLocalizer;
private readonly IWebHostEnvironment _environment;
public FlightsController(ILogger<FlightsController> logger,
IStringLocalizer stringLocalizer,
IWebHostEnvironment environment)
{
_logger = logger;
_stringLocalizer = stringLocalizer;
_environment = environment;
}
[HttpPost("OrderFlight")]
public async Task<IActionResult> OrderFlight([FromBody] OrderFlightBindingModel model)
{
return Ok(model);
}
[HttpPut("OrderFlight")]
public async Task<IActionResult> ValidateOtp([FromBody] CompleteOrderBindingModel model)
{
return Ok(model);
}
}
启动服务配置sn -p:
//Configure Services
services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Authority = _config["JWT:Authority"];
options.Audience = _config["JWT:Audience"];
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
ValidateIssuer = true,
ValidateLifetime = true,
ValidateAudience = true
};
});
services.AddAuthorization();
启动配置sn-p:
//Configure
app.UseAuthentication();
app.UseAuthorization();
欢迎任何想法。我对 .Net Core API 非常熟悉,所以假设我已经正确完成了相关的 appsettings 工作,并且我没有错过任何事情,因为总共有 12 个左右的端点,并且只有一个 PUT 操作返回 401,即使auth 应用于整个控制器,而不是基于每个方法。我正在使用 VS Professional 和 Kestrel 在 Localhost 上进行调试。
【问题讨论】:
标签: c# asp.net-core .net-core jwt flurl