下面是进行身份验证的代码,生成Authorization标头并调用API.
不幸的是,我在API上发出请求后收到401 Unauthorized
错误GET
.
但是,当我捕获Fiddler中的流量并重放它时,对API的调用成功,我可以看到所需的200 OK
状态代码.
[Test]
public void RedirectTest()
{
HttpResponseMessage response;
var client = new HttpClient();
using (var authString = new StringContent(@"{username: ""theUser"", password: ""password""}", Encoding.UTF8, "application/json"))
{
response = client.PostAsync("http://host/api/authenticate", authString).Result;
}
string result = response.Content.ReadAsStringAsync().Result;
var authorization = JsonConvert.DeserializeObject<CustomAutorization>(result);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorization.Scheme, authorization.Token);
client.DefaultRequestHeaders.Add("Accept", "application/vnd.host+json;version=1");
response =
client.GetAsync("http://host/api/getSomething").Result;
Assert.True(response.StatusCode == HttpStatusCode.OK);
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,Authorization标头丢失.
但是,在Fiddler中,标题成功传递.
知道我做错了什么吗?
在Chrome,IE和Firefox中,针对303响应进行重定向时,Authorization
会包含标头。
当对内部服务的请求在Location
标头中签名为S3 URL时,这就是一个问题。
S3将以400响应进行响应,并且无法确定要使用哪种身份验证方法。
要求内部服务
GET INTERNAL_SERVICE HTTP/1.1
Pragma: no-cache
Origin: https://example.com
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,en-US;q=0.8,da;q=0.7,de;q=0.6
Authorization: Bearer g6YQjOy3BDu32es8xKdMRNpcQ2Fkrh5NG7y5fDs5
Accept: application/json, text/plain, */*
Cache-Control: no-cache
Authority: example.com
Host: example.com
Connection: close
Run Code Online (Sandbox Code Playgroud)
响应
HTTP/1.1 303 See Other
Date: Tue, 13 Mar 2018 08:55:12 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Server: nginx
location: S3_SIGNED_URL
Cache-Control: no-cache, private
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type, Accept, Authorization, X-
Requested-With
Access-Control-Max-Age: 28800
Run Code Online (Sandbox Code Playgroud)
要求S3 …