I am trying to make an HttpWebRequest from an ASP.Net page on a server called SV-REQ against another IIS7 server called SV-RES. As a test I have added an entry to the credential cache for each auth type. When I set IIS to use Digest, Basic, or Negotiate on SV-REQ the code executes fine and I get a valid response from SV-RES confirming that the user credentials are correct.
However, when I set IIS to use "Windows Authentication" on SV-RES I get back a 401 error. Looking at the IIS log files I get a 401.2 followed immediately by a 401.1. I understand why I am getting the 401.2 but the 401.1 doesn't make sense since my code has credentials for each type and should get the appropriate one. Setting it to only have a single credential type, or even removing the credential cache altogether and passing the credential direct to the request also do not work.
Any help here would be greatly appreciated. Hopefully I am just missing something with the credentials bit...
This is the code on SV-REQ that is being executed. SV-REQ is IIS7 and is configured for ASP.Net 2.0
1 Dim credCache As CredentialCache = New CredentialCache()
2 Dim mUri As Uri = New Uri("http://sv-res.my-domain-here.com/default.htm
3 Dim mreq As HttpWebRequest = WebRequest.Create(mUri.ToS
4 credCache.Add(mUri, "NTLM", New NetworkCredential(muser, mpass, mdomain))
5 credCache.Add(mUri, "Negotiate", New NetworkCredential(muser, mpass, mdomain))
6 credCache.Add(mUri, "Basic", New NetworkCredential(muser, mpass, mdomain))
7 credCache.Add(mUri, "Digest", New NetworkCredential(muser, mpass, mdomain))
8 mreq.Credentials = credCache
9 Dim mres As HttpWebResponse = mreq.GetResponse
Here is the error I get back from SV-RES from the above code. SV-RES is also IIS7 configured for ASP.Net 2.0
The remote server returned an error: (401) Unauthorized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.WebException: The remote server returned an error: (401) Unauthorized.
Line 31: credCache.Add(mUri, "Digest", New NetworkCredential(muser, mpass, mdomain))
Line 32: mreq.Credentials = credCache
Line 33: Dim mres As HttpWebResponse = mreq.GetResponse
Line 34: Dim sr As StreamReader = New StreamReader(mres.GetRespo
Line 35: txtResult.Text = sr.ReadToEnd()
Source File: C:\inetpub\httproot\conten
.vb Line: 33
[WebException: The remote server returned an error: (401) Unauthorized.]
ct sender, EventArgs e) in C:\inetpub\httproot\conten
gs e) +132
int, Boolean includeStagesAfterAsyncPoi