• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1533
  • Last Modified:

HttpWebRequest with NTLM not working...

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.ToString)
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.

Source Error:

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.GetResponseStream())
Line 35:         txtResult.Text = sr.ReadToEnd()
 

Source File: C:\inetpub\httproot\contentscan.aspx.vb    Line: 33

Stack Trace:


[WebException: The remote server returned an error: (401) Unauthorized.]
   System.Net.HttpWebRequest.GetResponse() +1126
   contentscan.Page_Load(Object sender, EventArgs e) in C:\inetpub\httproot\contentscan.aspx.vb:33
   System.Web.UI.Control.OnLoad(EventArgs e) +132
   System.Web.UI.Control.LoadRecursive() +66
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428

0
lordicarus
Asked:
lordicarus
  • 3
1 Solution
 
GiftsonDJohnCommented:
Are you using Proxy Server for data communication?
0
 
lordicarusAuthor Commented:
No proxy.  What you see in the code above is all.  This seems to be an issue where the request is using the account under asp.net and not the one I am specifying in the credential cache.  Doesn't make sense.
0
 
lordicarusAuthor Commented:
Looks like it is related to this.... I am havent had a chance to test, but this seems to be the resolution.  http://msdn.microsoft.com/en-us/library/cc982052.aspx  Wanted to share with anyone else looking to solve the same problem.
0
 
lordicarusAuthor Commented:
I did some digging and it looks like my last post was the resolution.  I have gone into depth and posted the code I was using for this issue.

http://www.tinyint.com/index.php/2009/08/24/401-error-on-httpwebrequest-with-ntlm-authentication/
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now