我在嘗試使用當前用戶憑據對自身進行調用時在IIS上的MVC4中獲取經過驗證的響應時遇到問題。我正在使用下面的代碼。它只是使用端口返回給定頁面來創建一個請求回本地服務器。請注意,將'localhost'更改爲HttpContext.Request.Url.Host
沒有區別。IIS授權在WebRequest自行執行期間不工作
在我的本地機器上的Dev Server,這個工程。 在我的本地機器上使用IIS它沒有,我得到一個401:未經授權。
在IIS上,我在基本身份驗證和ASP.Net模擬上使用web.config中的<authentication mode="Windows" />
和<identity impersonate="true" />
。它是這樣配置的,以確保SQL Server(位於不同的計算機上)可以正確進行身份驗證。
由於服務器自己調用,我不相信這是雙跳問題?
有沒有人有任何建議如何獲得請求使用當前用戶身份驗證的服務器進行身份驗證?
[Authorize]
public ActionResult GetTest()
{
try
{
string urlAddress = string.Format("http://{0}:{1}/{2}", "localhost", HttpContext.Request.Url.Port, "api/diagnostic/get");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
request.Proxy = null; // Important! Stops the object searching for settings that cause a big slowdown.
request.Credentials = CredentialCache.DefaultCredentials;
request.PreAuthenticate = true;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream receiveStream = response.GetResponseStream())
{
if (receiveStream == null)
{
throw new Exception("ReceiveStream is null.");
}
StreamReader readStream = response.CharacterSet == null
? new StreamReader(receiveStream)
: new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
string data = readStream.ReadToEnd();
response.Close();
readStream.Close();
return Content(data);
}
}
}
return Content("Error");
}
catch (Exception e)
{
return Content(e.Message);
}
}
很可能您的服務器憑據無效。嘗試另一個憑據,而不是'request.Credentials = CredentialCache.DefaultCredentials;' –
@KhanhTO謝謝,我已經添加了一些說明,我需要使用當前用戶憑據進行調用。 – BJury
@KhanhTO但是,它可以使用模擬身份驗證SQL Server,Sharepoint和其他服務,但不能使用身份驗證嗎? – BJury