我正在尝试从名为 SV-REQ 的服务器上的 ASP.Net 页面针对另一台名为 SV-RES 的 IIS7 服务器创建 HttpWebRequest。当我在 SV-RES 上设置 IIS 以使用 Digest、Basic 或 Negotiate 并对凭据缓存对象进行更改以使用适当的方法时,代码执行良好,并且我从 SV-RES 获得有效响应,确认用户凭据是正确的。但是,当我将 IIS 设置为在 SV-RES 上使用“Windows 身份验证”时,在凭据缓存对象中使用 NTLM 时会返回 401 错误。
我真的很茫然,如果有人知道如何完成这项工作,我将不胜感激。
注意:执行此代码的服务器在通过 HTTP 的匿名身份验证下运行。获取请求的服务器是通过 HTTPS 的 NTLM(如前所述),如下面的代码所示。
这是正在执行的 SV-REQ 上的代码。SV-REQ 是 IIS7 并为 ASP.Net 2.0 配置
Dim credCache As CredentialCache = New CredentialCache()
Dim mUri As Uri = New Uri("https://sv-res.my-domain-here.com/default.htm")
Dim mreq As HttpWebRequest = WebRequest.Create(mUri.ToString)
credCache.Add(mUri, "NTLM", New NetworkCredential(muser, mpass, mdomain))
mreq.Credentials = credCache
Dim mres As HttpWebResponse = mreq.GetResponse
这是我从上述代码中从 SV-RES 得到的错误。SV-RES 也是为 ASP.Net 2.0 配置的 IIS7
远程服务器返回错误:(401) Unauthorized。 说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 异常详细信息:System.Net.WebException:远程服务器返回错误:(401)未经授权。 源错误: 第 31 行: credCache.Add(mUri, "NTLM", New NetworkCredential(muser, mpass, mdomain)) 第 32 行:mreq.Credentials = credCache 第 33 行:将 mres 调暗为 HttpWebResponse = mreq.GetResponse 第 34 行:Dim sr As StreamReader = New StreamReader(mres.GetResponseStream()) 第 35 行:txtResult.Text = sr.ReadToEnd() 源文件:C:\inetpub\httproot\contentscan.aspx.vb 行:33 堆栈跟踪: [WebException:远程服务器返回错误:(401)未经授权。] System.Net.HttpWebRequest.GetResponse() +1126 C:\inetpub\httproot\contentscan.aspx.vb:33 中的 contentscan.Page_Load(Object sender, EventArgs e) System.Web.UI.Control.OnLoad(EventArgs e) +132 System.Web.UI.Control.LoadRecursive() +66 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+2428