ASP.NET - The request failed with HTTP status 401: Unauthorized.

Asked By Jimba Ittai on 18-Jun-11 06:37 PM
Earn up to 10 extra points for answering this tough question.
Hello,

I have a windows hosting plan on a server in the US. In there I have 2 virtual directories. One contains a webservice (asmx file) and the other an aspx page.

I also have a windows mobile app which needs to invoke a method of the webservice. This works great! No problems at all..

However, when I use  the exact same code for the credentials in the aspx page which calls the asmx web service, it returns The request failed with HTTP status 401: Unauthorized. It doesn't give any other value ie 401.1, 401.2 etc..

If I try to access the web service manually or through VS2008 it works great as well! The problem occurs only when I use an aspx page which invokes the asmx web service.

The code I use is in VB.net and is the following:

Public serviceA As New webreferenceservice.ServiceB

Dim mycreds As New System.Net.CredentialCache()

Dim creds As New System.Net.NetworkCredential("username", "pass")

mycreds.Add(New Uri("http://site.com/virtualdir/webservice.asmx"), "Negotiate", creds)

ServiceA.Credentials = mycreds

It works great on the mobile and I copied-pasted it into the aspx.vb file.

Things I have tried:

1. Added <identity impersonate="true" />
2. Tried ServiceA
.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials and ServiceA.Credentials = System.Net.CredentialCache.DefaultCredentials

I have read some things in the forums regarding the sharing options etc but the account I use is the only one my hosting provider allows and it is the one for remote management of the website through IIS. However IIS does not have any options regarding folder properties when I right click on the virtual folder, probably due to restrictions.

Could you please help me find a solution?

pete rainbow replied to Jimba Ittai on 19-Jun-11 07:18 PM
i read somewhere that

<quote>
it is possible that the Web server (or surrounding systems) have been configured to disallow certain patterns of HTTP traffic. In other words, HTTP communication from a well-known Web browser is allowed, but automated communication from other systems is rejected with an 401 error code. This is unusual, but may indicate a very defensive security policy around the Web server.
</quote>

also can you get access to the iis you are trying to connect to and see if it contains any more info?