Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTTPWebRequest - 401 Unauthorized Error

Status
Not open for further replies.

dfrazell

IS-IT--Management
May 26, 2005
65
US
I’m trying to run a VB.NET console application on a Windows 2003 server running IIS 6.0 and I can’t seem to get the authorization correct for it to run.

When I execute the console app from the server I get the following error:
The remote server returned an error: (401) Unauthorized.

What is interesting is that the console application will run successfully from my workstation against the same URL but when I run the console application from the server where the URL resides and where IIS is hosted it fails with the 401 unauthorized error. I believe it’s a permissions issue within IIS but I don’t know where to look or how to correct. How can I further troubleshoot this problem or resolve the issue?

ISS web site properties:
Under Directory Security, the Authentication Methods are set as follows:
Enable anonymous access - is unchecked
Integrated Windows authentication – Is checked


Code sample:
Code:
Try
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
__httpRequest = WebRequest.Create(__URL)
__httpRequest.PreAuthenticate = True
__httpRequest.Credentials = CredentialCache.DefaultNetworkCredentials

‘FAILS ON THIS LINE
__httpResponse = __httpRequest.GetResponse() 

__reader = New StreamReader(__httpResponse.GetResponseStream())
__writer.WriteLine(__reader.ReadToEnd())

__reader.Close()
__writer.Close()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Catch ex As WebException
Console.WriteLine(ex.Message)

If ex.Status = WebExceptionStatus.ProtocolError Then
Console.WriteLine("Status Code : {0}", CType(ex.Response, HttpWebResponse).StatusCode)
Console.WriteLine("Status Description : {0}", CType(ex.Response, HttpWebResponse).StatusDescription)
Console.Read()
End If

      Catch e As Exception
Console.WriteLine(e.Message)
End Try

IIS Log File - When ran from Server
– Notice there is no user name after the IP address
Code:
10.10.1.30, -, 2/22/2010, 11:08:38, W3SVC2054551701, SERVER1, 10.10.1.30, 15, 72, 1872, 401, 2148074254, GET, /inc/footer.htm, -,
10.10.1.30, -, 2/22/2010, 11:08:38, W3SVC2054551701, SERVER1, 10.10.1.30, 0, 131, 1750, 401, 0, GET, /inc/footer.htm, -,
10.10.1.30, -, 2/22/2010, 11:08:39, W3SVC2054551701, SERVER1, 10.10.1.30, 0, 299, 1521, 401, 2148074252, GET, /inc/footer.htm, -,

IIS Log File - When I ran it from my local workstation
-Notice the third line in the log has the domain and user name (I changed the actual domain and user name) and it runs successfully.
Code:
10.10.1.184, -, 2/22/2010, 16:48:03, W3SVC2054551701, SERVER1, 10.10.1.30, 15, 72, 1872, 401, 2148074254, GET, /inc/footer.htm, -,
10.10.1.184, -, 2/22/2010, 16:48:03, W3SVC2054551701, SERVER1, 10.10.1.30, 0, 131, 1750, 401, 0, GET, /inc/footer.htm, -,
10.10.1.184, DOMAIN\USERNAME, 2/22/2010, 16:48:03, W3SVC2054551701, SERVER1, 10.10.1.30, 0, 315, 679, 200, 0, GET, /inc/footer.htm, -,
 
I think I solved the problem on my own.

Disable the loopback check:
see
After I disabled the loopback on the server, I'm able to access the Intranet from a browser and my console app runs successfully.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top