2013-09-23 110 views
2

我有一個需要基本身份驗證標頭的web服務。然而,當我把它用重定向基本身份驗證

var header = "Authorization: Basic " + 
       CreateBasicHttpAuthenticationHeader(login, password); 
    webRequest.Headers.Add(header); 
    var webResponse = (HttpWebResponse)webRequest.GetResponse(); 

它返回一個303 - 參見其他:

POST https://myservice/rates HTTP/1.1 
Authorization: Basic QXZ...NjY= 
Content-Type: application/x-content 
X-API-Version: 1.1 
If-Unmodified-Since: Mon, 23 Sep 2013 08:32:27 GMT 
User-Agent: UserAgent 

響應:

HTTP/1.1 303 See Other 
Date: Mon, 23 Sep 2013 08:30:57 GMT 
X-Opaque-ID: q8nxxxxc 
Location: https://myservice/rates 
Content-Length: 0 

淨,然後自動發送一個GET請求到新位置:

GET https://myservice/rates HTTP/1.1 
Content-Type: application/x-content 
X-API-Version: 1.1 
If-Unmodified-Since: Mon, 23 Sep 2013 08:32:27 GMT 
User-Agent: UserAgent 

但是此次不會發送授權標題。你知道一種方法來告訴它發送所有呼叫的所有標題嗎?我應該告訴它不要遵循內容?

回答

2

您必須停用HttpRequest的AllowRedirect屬性。

然後,你必須建立你自己的重定向系統與基本身份驗證標題。

這並不美妙,但否則.Net框架在重定向時會丟失標題。