在 C# 中通过 Azure 流量管理器连接到 API 时出现 401 Unauthorized
401 Unauthorized when connecting to API through Azure Traffic Manager in C#
我有以下代码在 C# 中成功连接到第三方 API:
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(login.Username, login.Password);
var xml = client.DownloadString(url);
Debug.Write(xml);
}
这在直接连接到 API 时工作正常。但是,我正在尝试利用 Azure 流量管理器将负载分散到多个端点,并且在执行此操作时出现 401 Unauthorized 异常。使用 Postman 等工具并在请求中配置 Basic Auth 似乎可以正常工作。
我尝试将代码转换为 RestSharp,但它似乎具有相同的症状。
以下是 Fiddler 使用几种不同技术发出的请求:
C#/WebClient 直接到 API 端点(成功)
GET <ApiUrl> HTTP/1.1
Host: <ApiHost>
Connection: Keep-Alive
401 Unauthorized
GET <ApiUrl> HTTP/1.1
Authorization: Basic <AuthToken>
Host: <ApiHost>
C#/WebClient 到 Azure 流量管理器(401 未授权)
GET <TrafficManagerApiUrl> HTTP/1.1
Host: <TrafficManagerApiHost>
401 Unauthorized
GET <ApiUrl> HTTP/1.1
Host: <ApiHost>
Postman 到 Azure 流量管理器(成功)
GET <TrafficManagerApiUrl> HTTP/1.1
Host: <TrafficManagerApiHost>
Connection: keep-alive
Authorization: Basic <AuthToken>
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Postman-Token: 13396800-33ab-8d7b-664f-68b99e8f4ac1
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
302 Redirect
GET <ApiUrl> HTTP/1.1
Host: <ApiHost>
Connection: keep-alive
Authorization: Basic <AuthToken>
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Postman-Token: 13396800-33ab-8d7b-664f-68b99e8f4ac1
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: JSESSIONID=<jsessionid>
我没有正确处理 Azure 流量管理器的重定向。
这里有详细的答案:
我有以下代码在 C# 中成功连接到第三方 API:
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(login.Username, login.Password);
var xml = client.DownloadString(url);
Debug.Write(xml);
}
这在直接连接到 API 时工作正常。但是,我正在尝试利用 Azure 流量管理器将负载分散到多个端点,并且在执行此操作时出现 401 Unauthorized 异常。使用 Postman 等工具并在请求中配置 Basic Auth 似乎可以正常工作。
我尝试将代码转换为 RestSharp,但它似乎具有相同的症状。
以下是 Fiddler 使用几种不同技术发出的请求:
C#/WebClient 直接到 API 端点(成功)
GET <ApiUrl> HTTP/1.1
Host: <ApiHost>
Connection: Keep-Alive
401 Unauthorized
GET <ApiUrl> HTTP/1.1
Authorization: Basic <AuthToken>
Host: <ApiHost>
C#/WebClient 到 Azure 流量管理器(401 未授权)
GET <TrafficManagerApiUrl> HTTP/1.1
Host: <TrafficManagerApiHost>
401 Unauthorized
GET <ApiUrl> HTTP/1.1
Host: <ApiHost>
Postman 到 Azure 流量管理器(成功)
GET <TrafficManagerApiUrl> HTTP/1.1
Host: <TrafficManagerApiHost>
Connection: keep-alive
Authorization: Basic <AuthToken>
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Postman-Token: 13396800-33ab-8d7b-664f-68b99e8f4ac1
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
302 Redirect
GET <ApiUrl> HTTP/1.1
Host: <ApiHost>
Connection: keep-alive
Authorization: Basic <AuthToken>
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Postman-Token: 13396800-33ab-8d7b-664f-68b99e8f4ac1
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: JSESSIONID=<jsessionid>
我没有正确处理 Azure 流量管理器的重定向。
这里有详细的答案: