问题
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"
}
]
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);
}
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
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)]
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
回答1:
I ran into a similar issue. You could try "stringify"ing the data. That worked well for me.
来源:https://stackoverflow.com/questions/47237771/api-returns-unauthorized-when-doing-a-patch-from-javascript-client