Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The request failed with HTTP status 401: Unauthorized

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?

like image 451
Matt W Avatar asked Jul 19 '10 16:07

Matt W


People also ask

How do I fix request failed with HTTP status 401 unauthorized?

By default, the Web service client proxy does not inherit the credentials of the security context where the Web service client application is running. To resolve this problem, you must use the Credentials property of the Web service client proxy to set the security credentials for Web service client authentication.

Why do I get 401 unauthorized in Postman?

A 401 Unauthorized Error is an HTTP status code that indicates that the server received an unverified request because it lacks valid authentication credentials for the requested resource.


2 Answers

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.

like image 115
btlog Avatar answered Sep 19 '22 01:09

btlog


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

like image 29
Mohamad Chami Avatar answered Sep 22 '22 01:09

Mohamad Chami