The request failed with HTTP status 401:Access Denied
生活随笔
收集整理的這篇文章主要介紹了
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 |
有關如何設置憑據屬性的更多信息,請參閱本文中的“更多信息”部分。
回到頂端
狀態
這種現象是由設計導致的。回到頂端
更多信息
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 的匿名訪問。為此,請按照下列步驟操作:
| ||||||||||||||||||||
| 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 示例: //?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 步。 |
轉載于:https://www.cnblogs.com/wangtory/archive/2008/03/25/1120895.html
總結
以上是生活随笔為你收集整理的The request failed with HTTP status 401:Access Denied的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软免费图书《Introducing M
- 下一篇: 网页效果图设计之色彩索引