我正在对使用身份服务器进行身份验证的 API 进行修补。当我在邮递员上运行它时,补丁工作正常
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 上执行补丁时,它会返回未经授权的:
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
我遇到了类似的问题。您可以尝试"字符串化"数据。这对我来说效果很好。
这是最新的 .NET Core 3+ System.Text.Json 库的新问题;如果您从 Newtonsoft.Json 迁移到 System.Text.Json,现在的共识是将其切换回来。