图像从Angular 4上传到ASP.NET Api获取401

时间:2018-01-31 05:20:51

标签: asp.net angular api web

我有一个基于asp.net构建的API。我试图通过此API上传图像,但获得401响应。相同的API正在邮递员工作。 我使用Auth0,所以使用令牌但令牌不是问题,因为我尝试在我的角度网络应用程序中使用邮递员令牌和邮递员中的网络令牌,并且在两种情况下邮递员都可以工作,但网络不是。 这也不是允许原始URL的问题,因为所有其他api都在工作。 提琴手回应我通过Postman上传图片

  

请求标题

Content-Type: multipart/form-data; boundary=--------------------------930238038400236037556404
Authorization: bearer {token}
cache-control: no-cache
Postman-Token: e0914722-51ae-46ae-89e1-374083db2e7e
User-Agent: PostmanRuntime/7.1.1
Accept: */*
Host: Azurowebsite
cookie: ARRAffinity=85c14af7254dbffe56e4b0a1bcfc7587606ee9c95b1b694237067a43fe554677
accept-encoding: gzip, deflate
content-length: 90000
Connection: keep-alive
  

响应标题

HTTP/1.0 200 OK
Content-Type: application/json; charset=utf-8
ETag: "46008695-0000-0000-0000-5a714d080000"
Vary: Accept-Encoding
Server: Kestrel
X-Powered-By: ASP.NET
Date: Wed, 31 Jan 2018 04:58:47 GMT
X-Cache: MISS from gateway
X-Cache-Lookup: MISS from gateway
Via: 1.0 gateway.purelogics.net (squid/3.1.23)
Connection: close
Content-Length: 1570

来自网络的小提琴响应是

  

请求标题

Host: azurowebsite
Connection: keep-alive
Content-Length: 89976
Accept: Bearer {token}
Origin: testing server
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: application/json
Referer: testing server
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
  

响应标题

HTTP/1.0 401 Unauthorized
Server: Kestrel
WWW-Authenticate: Bearer
WWW-Authenticate: Bearer
WWW-Authenticate: Bearer
Access-Control-Allow-Origin: Testing server
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=85c14af7254dbffe56e4b0a1bcfc7587606ee9c95b1b694237067a43fe554677;Path=/;HttpOnly;Domain=zytoproductxwebapi-testing.azurewebsites.net
Date: Wed, 31 Jan 2018 04:59:04 GMT
Content-Length: 0
X-Cache: MISS from gateway
X-Cache-Lookup: MISS from gateway
Via: 1.0 gateway.purelogics.net (squid/3.1.23)
Connection: keep-alive

1 个答案:

答案 0 :(得分:-1)

<强>解决即可。万一有人面临这个问题。 问题不在API中,也不在代码中。问题是这个ngx-uploader包不允许在标题部分发送令牌。我知道它很奇怪,包作者应该允许这个。 我花了两天时间寻找这个错误的解决方案。事实证明,此程序包仅允许在此自定义参数

中发送令牌
authToken: token

而不是

header : {Authorization : token}

所以我调用API的简短代码是

public uploaderOptions: NgUploaderOptions = {
url: this.url,
method: 'POST',
filterExtensions: true,
allowedExtensions: ['jpg', 'png'],
authToken   : localStorage.getItem('id_token'),

};