简体   繁体   中英

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?

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.

在IIS 7中,启用匿名身份验证,您应该能够进行调试。

webService.UseDefaultCredentials = true

这对我有用。

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

im am test this way:

CheckListService.CheckList chkSrvice = new CheckListService.CheckList() {
    UseDefaultCredentials = true };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM