从javascript客户端执行补丁程序时,API返回未授权 [英] API Returns Unauthorized when doing a patch from javascript client

查看:187
本文介绍了从javascript客户端执行补丁程序时,API返回未授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对使用Identity Server进行身份验证的API进行补丁.当我在Postman上运行该补丁时,效果很好

I'm doing a patch to an API that uses Identity Server for authentication. When I run it on Postman the patch works perfectly

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:

But when I execute the patch on my Reactjs using axios it would return 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设置,可以接受来自此端口的请求:

I have my Cors setup to accept requests from this port:

services.AddCors(options =>
            {

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

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

So I'm not sure what else I'm missing.

编辑 修复了邮递员的标题

EDIT Fixed the header for postman

我在Identity Server日志上看到的错误

Error I see on the Identity Server logs

[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的另一个日志输出

Here's another log output from 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 

推荐答案

这是用于.NET Core 3+的最新System.Text.Json库的新问题.如果您从Newtonsoft.Json迁移到System.Text.Json,则目前的共识是将其切换回去.

This is newly an issue with the latest System.Text.Json library for .NET Core 3+; if you migrated from Newtonsoft.Json to System.Text.Json, the consensus for now is to switch it back.

这篇关于从javascript客户端执行补丁程序时,API返回未授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆