The request failed with HTTP status 401:Access Denied

症状

当您尝试调用 Web 服务应用程序并且匿名访问身份验证处于关闭状态时,可能会收到以下错误信息。 
The request failed with HTTP status 401:Access Denied.

Description:An unhandled exception occurred during the execution of the current Web request.Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.Net.WebException:The request failed with HTTP status 401:Access Denied.

回到顶端

原因

当匿名访问身份验证对 Web 服务应用程序关闭时,所有的调用方应用程序在发出任何请求之前必须提供凭据。默认情况下,Web 服务客户端代理不继承运行 Web 服务客户端应用程序的安全上下文的凭据。

回到顶端

解决方案

要解决这个问题,必须使用 Web 服务客户端代理的凭据属性为 Web 服务客户端身份验证设置安全凭据。

要设置凭据属性,请执行下列操作之一:
第一种方法
DefaultCredentials 分配给 Web 服务代理类的凭据属性,以在匿名访问身份验证关闭时调用 Web 服务。CredentialCache 类的 DefaultCredentials 属性提供运行应用程序的安全上下文的系统凭据。为此,请使用以下代码:

Visual C# .NET 示例
            
//Assigning DefaultCredentials to the Credentials property
//of the Web service client proxy (myProxy).
myProxy.Credentials= System.Net.CredentialCache.DefaultCredentials;
Visual Basic .NET 示例
 
            
'Assigning DefaultCredentials to the Credentials property
'
of the Web service client proxy (myProxy).
myProxy.Credentials= System.Net.CredentialCache.DefaultCredentials
第二种方法
您还可以使用 CredentialCache 类为 Web 服务客户端身份验证提供凭据。创建 CredentialCache 类的实例。创建使用指定的用户名、密码和域的 NetworkCredential 的实例。将 NetworkCredential 添加到具有身份验证类型的 CredentialCache 类。为此,请使用以下代码:

Visual C# .NET 示例
            
//Create an instance of the CredentialCache class.
CredentialCache cache = new CredentialCache();

// Add a NetworkCredential instance to CredentialCache.
// Negotiate for NTLM or Kerberos authentication.
cache.Add( new Uri(myProxy.Url), "Negotiate"new NetworkCredential("UserName""Password""Domain")); 

//Assign CredentialCache to the Web service Client Proxy(myProxy) Credetials property.
myProxy.Credentials = cache;

Visual Basic .NET 示例
            
'Create an instance of the CredentialCache class.
Dim cache As CredentialCache = New CredentialCache()

'Add a NetworkCredential instance to CredentialCache.
'
Negotiate for NTLM or Kerberos authentication.
cache.Add(New Uri(myProxy.Url), "Negotiate"New NetworkCredential("UserName""Password""Domain"))

'Assign CredentialCache to the Web service Client Proxy(myProxy) Credetials property.
myProxy.Credentials = cache
注意CredentialCache 类和 NetworkCredential 类属于 System.Net 命名空间。

有关如何设置凭据属性的更多信息,请参阅本文中的“更多信息”部分。

回到顶端

状态

这种现象是由设计导致的。

回到顶端

更多信息

DefaultCredentials 代表运行应用程序的当前安全上下文的系统凭据。对于客户端的应用程序,默认凭据通常是 Windows 凭据,例如运行该程序的用户的用户名、密码和域。对于 ASP.NET 程序,默认凭据是 ASP.NET 辅助进程的标识(或者正在模拟的用户)的用户凭据。在以下示例 ASP.NET 程序中,DefaultCredentials 代表 ASPNET 用户帐户(对于在 Microsoft Internet 信息服务 [IIS] 6.0 上运行的应用程序,则是 NetworkService 用户帐户),因为没有对调用方设置模拟。

回到顶端

重现此问题的步骤

1. 使用 Visual C# .NET 或 Visual Basic .NET 创建新的“ASP.NET Web 服务”。
2. 将该项目命名为“WebServiceTest”。
3. 默认情况下,会创建“Service1.asmx”。
4. 取消默认 WebMethod“HelloWorld()”的备注标记。
5. 在“生成”菜单上,单击“生成解决方案”。
6. 关闭对 WebServiceTest 的匿名访问。为此,请按照下列步骤操作:
a. 在“控制面板”中,双击“管理工具”。
b. 双击“Internet 信息服务”。
c. 展开“Internet 信息服务”,然后找到“WebServiceTest”虚拟目录。
d. 右键单击“WebServiceTest”,然后单击“属性”。
e. 单击“目录安全性”选项卡。
f. 在“匿名访问和验证控件”下,单击“编辑”。
g. 在“身份验证方法”对话框中,单击以清除“匿名访问”复选框。
h. 单击以选中“集成 Windows 验证”复选框。

注意:确认只选中了“集成 Windows 验证”。
i. 单击“确定”以关闭“身份验证方法”对话框。
j. 单击“确定”以关闭“属性”。
7. 在“生成”菜单上,单击“生成解决方案”。
8. 在浏览器中键入下面的地址以查看“Service1”Web 服务说明:
http://localhost/WebServiceTest/Service1.asmx
9. 测试 HelloWorld WebMethod。WebMethod 能够如期工作。
10. 将一个 Web 引用添加到测试“ASP.NET Web 应用程序”。为此,请按照下列步骤操作:
a. 使用 Visual C# .NET 或 Visual Basic .NET 创建新的“ASP.NET Web 应用程序”。将该项目命名为“WebServiceCaller”。
b. 默认情况下,会创建“WebForm1.aspx”。
c. 在解决方案资源管理器中,右键单击“引用”,然后单击“添加 Web 引用”。
d. 在“地址”文本框中,键入 WebServiceTest 的 URL,如下所示:
http://localhost/WebServiceTest/Service1.asmx
e. 单击“转到”或按 Enter 键,然后单击“添加应用”。
11. 在解决方案资源管理器中,右键单击“WebForm1.aspx”,然后单击“查看代码”。
12. 将以下代码附加到 Page_Load 事件:

Visual C# .NET 示例:
            
// Start an instance of the Web service client-side proxy.
localhost.Service1 myProxy = new localhost.Service1();
Response.Write( myProxy.HelloWorld());
Visual Basic .NET 示例:
            
'Start an instance of the Web service client-side proxy.
Dim myProxy As localhost.Service1 = New localhost.Service1()
Response.Write(myProxy.HelloWorld())
13. 在“调试”菜单上,单击“开始”,然后在浏览器中查看应用程序。
14. “症状”部分讨论的错误信息将会出现在浏览器中。
15. 要解决此问题,请将 DefaultCredentials 分配给 Web 服务客户端代理的 Credentials 属性。为此,请在行“Response.Write( myProxy.HelloWorld())”之前插入以下代码:

Visual C# .NET 示例:
            
myProxy.Credentials= System.Net.CredentialCache.DefaultCredentials;
Visual Basic .NET 示例:
            
myProxy.Credentials = System.Net.CredentialCache.DefaultCredentials
16. 重复第 13 步。
posted on 2008-03-25 10:56  北漂浪子  阅读(2705)  评论(1编辑  收藏  举报