【问题标题】:API Returns Unauthorized when doing a patch from javascript client从 javascript 客户端执行补丁时 API 返回未经授权
【发布时间】:2020-02-27 09:39:46
【问题描述】:

我正在对使用 Identity Server 进行身份验证的 API 进行修补。当我在 Postman 上运行它时,补丁可以完美运行

http://localhost:90909/api/products/3434

Headers =  { 
   Authorization: Bearer <token>
   Content-Type: application/json-patch+json
}

Body: 
[
    {
     "op" : "replace",
     "path" : "/DESCRIPTION",
     "value" : "bruhhh"
    }
]

但是当我使用 axios 在我的 Reactjs 上执行补丁时,它会返回 Unauthorized:

static update(data){
        let config = {
            data : [],
            headers: { 
                'Authorization' : 'Bearer ' + data.access_token,
                'Content-Type' : 'application/json-patch+json'           
            }     
        }

        config.data.push(
                {
                 "op" : "replace",
                 "path" : "/DESCRIPTION",
                 "value" : "you da best"
                }

        )

        return axios.patch(root + '/api/products/' + data.product.id, config);

}

我的 Cors 设置可以接受来自此端口的请求:

services.AddCors(options =>
            {

                options.AddPolicy("JSClient", builder =>
                    builder.WithOrigins("http://localhost:9999")
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
            });

所以我不确定我还缺少什么。

编辑 修复了邮递员的标题

我在身份服务器日志中看到的错误

[msg=Log Error];[msg=Exception while initializing Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.ClientIpHeaderTelemetryInitializer, exception message - System.TypeInitializationException: The type initializer for 'Microsoft.ApplicationInsights.AspNetCore.Extensibility.Implementation.Tracing.AspNetCoreEventSource' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.PlatformAbstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
   at Microsoft.ApplicationInsights.AspNetCore.Extensibility.Implementation.Tracing.AspNetCoreEventSource..ctor()
   at Microsoft.ApplicationInsights.AspNetCore.Extensibility.Implementation.Tracing.AspNetCoreEventSource..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.ClientIpHeaderTelemetryInitializer.OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
   at Microsoft.ApplicationInsights.TelemetryClient.Initialize(ITelemetry telemetry)]

这是 serilog 的另一个日志输出

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 OPTIONS http://localhost:90909/api/products/3434  
info: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[4]
      Policy execution successful.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 15.8032ms 204 
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 PATCH http://localhost:90909/api/products/3434 application/json;charset=UTF-8 805
info: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[4]
      Policy execution successful.
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
      Authorization failed for user: (null).
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
      Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
info: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
      Executing ChallengeResult with authentication schemes ().
info: Microsoft.AspNetCore.Builder.IdentityServerAuthenticationHandler[12]
      AuthenticationScheme: Bearer was challenged.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action InventoryApi.Controllers.ProductsController.Update (InventoryApi) in 46.6963ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 127.3384ms 401 

【问题讨论】:

  • 您是否尝试过在 API 端调试问题?
  • 两者的标题Bearer格式不一样吗?
  • 在邮递员中也应该像 Authorization:Bearer {{token}} 如果您使用的是基本身份验证(如在反应代码中)
  • 我刚刚调试了它。只有一个错误,我更新了帖子。还更新了邮递员请求的外观

标签: axios identityserver4


【解决方案1】:

我遇到了类似的问题。您可以尝试“字符串化”数据。这对我来说效果很好。

【讨论】:

    【解决方案2】:

    这是 .NET Core 3+ 的最新 System.Text.Json 库的新问题;如果您从 Newtonsoft.Json 迁移到 System.Text.Json,目前的共识是切换回来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 2016-10-13
      • 2021-01-07
      • 1970-01-01
      • 2016-12-24
      相关资源
      最近更新 更多