![](/img/trans.png)
[英]StatusCode: 401, ReasonPhrase: 'Unauthorized' gets displayed when calling a Post Method through HTTPClient using C#
[英]C# PostAsync - {StatusCode: 401, ReasonPhrase: 'Unauthorized' with ASP.NET MVC 4
我正在使用ASP.NET MVC4並使用PostAsync調用控制器中的一個方法,從一個MVC4項目到另一個MVC4項目。 我沒有在被調用的控制器或方法中使用任何身份驗證屬性。
引用此鏈接: 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
{
//
}
}
問題似乎是由於從承載IIS 5.1或更高版本的同一服務器訪問本地Web應用程序並使用完全限定的域名。
FQDN的域名需要在注冊表中列出白名單,我可以通過完成以下步驟來完成此操作:
在進行任何調整之前,最好先創建一個注冊表備份
1)單擊“ 開始” ,單擊“運行” ,鍵入regedit,然后單擊“ 確定” 。
2)在注冊表編輯器中,找到並單擊以下注冊表項: HKEY_LOCAL_MACHINE \\ SYSTEM \\ CurrentControlSet \\ Control \\ Lsa \\ MSV1_0
3)右鍵單擊“ MSV1_0” ,指向“ 新建” ,然后單擊“ 多字符串值” 。
4)鍵入BackConnectionHostNames,然后按Enter。
5)右鍵單擊“ BackConnectionHostNames” ,然后單擊“修改”。
6)在“ 數值數據”框中,鍵入本地計算機上站點的主機名或主機名,然后單擊“ 確定” 。
7)退出注冊表編輯器,然后重新啟動IISAdmin服務。
有關更多詳細信息,請參閱: https : //support.microsoft.com/en-us/help/896861/you-receive-error-401.1-when-you-browse-a-web-site-that-uses-integrate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.