【问题标题】:Unexplained 401 response from PUT method on .Net Core 3.1.Net Core 3.1 上的 PUT 方法出现无法解释的 401 响应
【发布时间】:2021-05-18 09:15:03
【问题描述】:

我有一个非常有趣的问题。我有一个 .Net Core 3.1 Rest Web API。背景资料:

  1. 我已根据我们的其他微服务正确配置了身份验证。
  2. POST 正确验证并返回 200 OK
  3. PUT 操作返回 401 UnAuthorized
  4. 我在逐个路由的身份验证方面什么都不做

控制器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


    【解决方案1】:

    您不需要使用 Put,因为某些浏览器不支持它。修复动作标题:

       [Route("OrderFlight")]
        public async Task<IActionResult> OrderFlight([FromBody] OrderFlightBindingModel model)
        {
            return Ok(model);
        }
    
        [Route("ValidateOtp")]
        public async Task<IActionResult> ValidateOtp([FromBody] CompleteOrderBindingModel model)
        {
            return Ok(model);
        }
    and use OrderFlight or ValidateOtp in your url.
    

    【讨论】:

    • 这是一个有效的建议。但是,它甚至无法在移动设备或 .Net 控制台应用程序中运行的集成测试中运行。只有帖子有效。
    • 你需要 Post 做什么?相信我。我从不明确使用这些方法。让浏览器选择一个方法。浏览器通常比开发人员更聪明。例如,浏览器通常选择 GET 进行删除,因为该操作通常看起来像 ... delete(int id)。如果你把 delete 属性放在某些情况下你也会有问题
    • @CaptainKenpachi - 但如果你喜欢阅读教科书并创建单元测试,请尝试 [HttpPut("ValidateOtp")]。它可能适用于单元测试。
    【解决方案2】:

    好吧,我仍然无法解释这种行为,但至少我学到了一两件事:

    通过 FlurlHttp 发送 PUT 请求返回 401。通过 PostMan 发送完全相同的请求按预期工作。我检查了三次,标题、URL、方法和正文都是相同的。所以

    所以,这是我必须向 Flurl 开发人员提出的错误。由于 PUT 请求来自 PostMan,我相信它也可以像所有其他服务的 PUT OTP 请求一样工作,该请求共享相同的设置并且一直在我们的 UAT 和生产环境中工作。因此,我将创建一个 POST 端点来明确测试它(这在技术上是一件坏事,但最终我更关心工作软件而不是 DRY 原则)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-16
      • 2020-05-10
      • 2023-03-08
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      相关资源
      最近更新 更多