我正在使用ASP.NET MVC4並使用PostAsync從一個MVC4項目控制器中調用另一個MVC4項目。我沒有在被調用的控制器或方法中使用任何身份驗證屬性。C#PostAsync - {StatusCode:401,ReasonPhrase:'未經授權'使用ASP.NET MVC 4
引用此鏈接:link 解決了我在本地運行應用程序並向服務器上的應用程序調用PostAsync時遇到的問題。現在,當我將應用程序部署到服務器並嘗試調用其他應用程序時,我收到了未經授權的錯誤。我究竟做錯了什麼?
在IIS中運行的應用程序都使用相同的站點和相同的應用程序池。
使用遠程調試,這是服務器響應:
{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Cache-Control: private
Server: Microsoft-IIS/7.0
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
Date: Fri, 23 Jun 2017 13:44:19 GMT
Content-Length: 6644
Content-Type: text/html; charset=utf-8
}}
這是我的代碼:
using (var handler = new HttpClientHandler { UseDefaultCredentials = true })
using (HttpClient client = new HttpClient(handler) { BaseAddress = new Uri(Properties.Settings.Default.EnvironmentRootPath)})
{
List<string> orderNumbers = NumList;
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("orderNumbers", string.Join(",", orderNumbers))
};
var content = new FormUrlEncodedContent(pairs);
// call sync
var response = client.PostAsync("CentralBroadcast/broadcastNewOrder", content).Result;
if (response.IsSuccessStatusCode)
{
//
}
else
{
//
}
}
您的應用程序已認證/授權實施?如果不是那麼最有可能的webproxy身份驗證問題。您可以嘗試在HttpClientHandler上設置屬性UseProxy = false。 –
是的,它使用Windows身份驗證。授權檢查在關鍵區域完成,但尚未在此控制器上完成。 – eaglei22
「UseDefaultCredentials = true」在從Visual Studio運行的本地服務器調用服務器上的應用程序時使所有工作都正常,但現在兩個應用程序在服務器上都存在問題。 – eaglei22