API返回从javascript客户端执行修补程序时未经授权

时间:2017-11-11 12:15:55

标签: axios identityserver4

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

Who of the following have won the Nobel Prize in Chemistry 2017?
[A] Jean-Pierre Sauvage, Fraser Stoddart and Ben Feringa[B] Tomas Lindahl and Paul L. Modrich[C] Brian K. Kobilka and Robert J. Lefkowitz[D] Jacques Dubochet, Joachim Frank and Richard Henderson

Correct Answer: D [Jacques Dubochet, Joachim Frank and Richard Henderson]
Answer Explanation:The Nobel Prize in Chemistry 2017 was awarded to Jacques Dubochet, Joachim Frank and Richard Henderson for developing cryo-electron microscopy for the high-resolution structure determination of biomolecules in solution. It’s a method of simplifying and improving the imaging of biomolecules.

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

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

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

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

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

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

}

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

修改 修复了邮递员的标题

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

services.AddCors(options =>
            {

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

这是来自serilog的另一个日志输出

[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)]

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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