The request failed with HTTP status 401: Unauthorized
Asked Answered
C

5

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?

Compte answered 19/7, 2010 at 16:5 Comment(0)
U
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.

Unexacting answered 19/7, 2010 at 16:14 Comment(3)
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.Lashoh
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.Sisyphus
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.Sod
C
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

Chambless answered 27/7, 2016 at 13:56 Comment(2)
I am also facing the same issuePanthia
I had the same issue and your solution worked for me thank you!Danny
C
3

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

Cheadle answered 1/10, 2012 at 20:59 Comment(0)
I
3
webService.UseDefaultCredentials = true

This worked for me.

Ingrain answered 4/6, 2013 at 21:44 Comment(0)
P
0

im am test this way:

CheckListService.CheckList chkSrvice = new CheckListService.CheckList() {
    UseDefaultCredentials = true };
Profant answered 29/6, 2014 at 4:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.