症状
<script type="text/javascript">loadTOCNode(1, 'symptoms');</script>
当您尝试调用 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.
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.
原因
<script type="text/javascript">loadTOCNode(1, 'cause');</script>
当匿名访问身份验证对 Web 服务应用程序关闭时,所有的调用方应用程序在发出任何请求之前必须提供凭据。默认情况下,Web 服务客户端代理不继承运行 Web 服务客户端应用程序的安全上下文的凭据。
解决方案
<script type="text/javascript">loadTOCNode(1, 'resolution');</script>
要解决这个问题,必须使用 Web 服务客户端代理的
凭据属性为 Web 服务客户端身份验证设置安全凭据。
要设置 凭据属性,请执行下列操作之一:
注意:
CredentialCache 类和
NetworkCredential 类属于
System.Net 命名空间。
有关如何设置 凭据属性的更多信息,请参阅本文中的“更多信息”部分。
要设置 凭据属性,请执行下列操作之一:
• | 第一种方法 将 DefaultCredentials 分配给 Web 服务代理类的凭据属性,以在匿名访问身份验证关闭时调用 Web 服务。CredentialCache 类的 DefaultCredentials 属性提供运行应用程序的安全上下文的系统凭据。为此,请使用以下代码: Visual C# .NET 示例 Visual Basic .NET 示例 |
• | 第二种方法 您还可以使用 CredentialCache 类为 Web 服务客户端身份验证提供凭据。创建 CredentialCache 类的实例。创建使用指定的用户名、密码和域的 NetworkCredential 的实例。将 NetworkCredential 添加到具有身份验证类型的 CredentialCache 类。为此,请使用以下代码: Visual C# .NET 示例 Visual Basic .NET 示例 |
有关如何设置 凭据属性的更多信息,请参阅本文中的“更多信息”部分。
状态
<script type="text/javascript">loadTOCNode(1, 'status');</script>
这种现象是由设计导致的。
更多信息
<script type="text/javascript">loadTOCNode(1, 'moreinformation');</script>
DefaultCredentials 代表运行应用程序的当前安全上下文的系统凭据。对于客户端的应用程序,默认凭据通常是 Windows 凭据,例如运行该程序的用户的用户名、密码和域。对于 ASP.NET 程序,默认凭据是 ASP.NET 辅助进程的标识(或者正在模拟的用户)的用户凭据。在以下示例 ASP.NET 程序中,
DefaultCredentials 代表 ASPNET 用户帐户(对于在 Microsoft Internet 信息服务 [IIS] 6.0 上运行的应用程序,则是 NetworkService 用户帐户),因为没有对调用方设置模拟。
重现此问题的步骤
<script type="text/javascript">loadTOCNode(2, 'moreinformation');</script>1. | 使用 Visual C# .NET 或 Visual Basic .NET 创建新的“ASP.NET Web 服务”。 | ||||||||||||||||||||
2. | 将该项目命名为“WebServiceTest”。 | ||||||||||||||||||||
3. | 默认情况下,会创建“Service1.asmx”。 | ||||||||||||||||||||
4. | 取消默认 WebMethod“HelloWorld()”的备注标记。 | ||||||||||||||||||||
5. | 在“生成”菜单上,单击“生成解决方案”。 | ||||||||||||||||||||
6. | 关闭对 WebServiceTest 的匿名访问。为此,请按照下列步骤操作:
| ||||||||||||||||||||
7. | 在“生成”菜单上,单击“生成解决方案”。 | ||||||||||||||||||||
8. | 在浏览器中键入下面的地址以查看“Service1”Web 服务说明:
http://localhost/WebServiceTest/Service1.asmx
| ||||||||||||||||||||
9. | 测试 HelloWorld WebMethod。WebMethod 能够如期工作。 | ||||||||||||||||||||
10. | 将一个 Web 引用添加到测试“ASP.NET Web 应用程序”。为此,请按照下列步骤操作:
| ||||||||||||||||||||
11. | 在解决方案资源管理器中,右键单击“WebForm1.aspx”,然后单击“查看代码”。 | ||||||||||||||||||||
12. | 将以下代码附加到 Page_Load 事件: Visual C# .NET 示例: Visual Basic .NET 示例: | ||||||||||||||||||||
13. | 在“调试”菜单上,单击“开始”,然后在浏览器中查看应用程序。 | ||||||||||||||||||||
14. | “症状”部分讨论的错误信息将会出现在浏览器中。 | ||||||||||||||||||||
15. | 要解决此问题,请将 DefaultCredentials 分配给 Web 服务客户端代理的 Credentials 属性。为此,请在行“Response.Write( myProxy.HelloWorld())”之前插入以下代码: Visual C# .NET 示例: Visual Basic .NET 示例: | ||||||||||||||||||||
16. | 重复第 13 步。 |