15

I have a .NET 2.0 website (VB) running in my IIS6 (XP Pro SP2) and a .NET 3.5 (configured as .NET2 under IIS's ASP.NET tab, of course) hosting an ASMX web service.

In Chrome, I can call the ASMX and invoke the web methods successfully. However, in calling the web methods in code, from the .NET 2.0 website I get:

The request failed with HTTP status 401: Unauthorized.

How do I get around this?

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
Matt W
  • 11,753
  • 25
  • 118
  • 215

5 Answers5

22

You need to set the credentials in you application when you initialise the webService object.

Something like webService.UseDefaultCredentials = true

This will set the credentials of the request to the current user executing the application.

youwhut
  • 948
  • 3
  • 17
  • 37
btlog
  • 4,760
  • 2
  • 29
  • 38
  • This is for the cient created in VB/C# .. what about Java, say I need to connect to this webservice through mobile application. In that case, I can't get the Default credentials. – rDroid Aug 28 '13 at 10:30
  • you saved lot of time. Thank you. – DelphiLearner Apr 04 '16 at 15:13
  • Oh my God. We've had code in our ASP.Net site without this "UseDefaultCredentials" setting for years, and it's always worked fine. Yesterday, a Microsoft security patch was applied suddenly we got a "401 Unauthorized" error each time. Adding this one line of code fixed the problem... really strange. – Mike Gledhill Mar 23 '17 at 09:47
  • This will probably work when developing the application. However, be careful when deploying it as you may not have the authentication being made in the deployment machine, in which case you'll need to set them as mentioned in [Mohamad Chami's answer](https://stackoverflow.com/a/38615047/214639). – Narcís Calvet Nov 16 '17 at 10:04
4

you can use this:

webservice.UseDefaultCredentials = true;

if does not work, use the below code instead of the code above

webservice.Credentials = new NetworkCredential("userName", "password", "domain");
webservice.PreAuthenticate = true;

note: the username Password and domain is the user credential of the user that access to webservice

so make sure that user have permission to access to web service

maybe the user is the windows user

and you can get the domain from: right click in "MyComputer" and properties the domain is the Computer name or Workgroup

Mohamad Chami
  • 1,226
  • 14
  • 10
3

In IIS 7, enable anonymous authentication and you should be able to debug.

Glade Mellor
  • 1,326
  • 17
  • 10
3
webService.UseDefaultCredentials = true

This worked for me.

Dragan Okanovic
  • 7,509
  • 3
  • 32
  • 48
SinghJi
  • 39
  • 4
0

im am test this way:

CheckListService.CheckList chkSrvice = new CheckListService.CheckList() {
    UseDefaultCredentials = true };
khagler
  • 3,996
  • 29
  • 40
ali
  • 1