日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

The request failed with HTTP status 401:Access Denied

發(fā)布時間:2025/7/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 The request failed with HTTP status 401:Access Denied 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

癥狀

當(dāng)您嘗試調(diào)用 Web 服務(wù)應(yīng)用程序并且匿名訪問身份驗(yàn)證處于關(guān)閉狀態(tài)時,可能會收到以下錯誤信息。?
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.

回到頂端

原因

當(dāng)匿名訪問身份驗(yàn)證對 Web 服務(wù)應(yīng)用程序關(guān)閉時,所有的調(diào)用方應(yīng)用程序在發(fā)出任何請求之前必須提供憑據(jù)。默認(rèn)情況下,Web 服務(wù)客戶端代理不繼承運(yùn)行 Web 服務(wù)客戶端應(yīng)用程序的安全上下文的憑據(jù)。

回到頂端

解決方案

要解決這個問題,必須使用 Web 服務(wù)客戶端代理的憑據(jù)屬性為 Web 服務(wù)客戶端身份驗(yàn)證設(shè)置安全憑據(jù)。

要設(shè)置憑據(jù)屬性,請執(zhí)行下列操作之一:
?第一種方法
DefaultCredentials 分配給 Web 服務(wù)代理類的憑據(jù)屬性,以在匿名訪問身份驗(yàn)證關(guān)閉時調(diào)用 Web 服務(wù)。CredentialCache 類的 DefaultCredentials 屬性提供運(yùn)行應(yīng)用程序的安全上下文的系統(tǒng)憑據(jù)。為此,請使用以下代碼:

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 服務(wù)客戶端身份驗(yàn)證提供憑據(jù)。創(chuàng)建 CredentialCache 類的實(shí)例。創(chuàng)建使用指定的用戶名、密碼和域的 NetworkCredential 的實(shí)例。將 NetworkCredential 添加到具有身份驗(yàn)證類型的 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 命名空間。

有關(guān)如何設(shè)置憑據(jù)屬性的更多信息,請參閱本文中的“更多信息”部分。

回到頂端

狀態(tài)

這種現(xiàn)象是由設(shè)計導(dǎo)致的。

回到頂端

更多信息

DefaultCredentials 代表運(yùn)行應(yīng)用程序的當(dāng)前安全上下文的系統(tǒng)憑據(jù)。對于客戶端的應(yīng)用程序,默認(rèn)憑據(jù)通常是 Windows 憑據(jù),例如運(yùn)行該程序的用戶的用戶名、密碼和域。對于 ASP.NET 程序,默認(rèn)憑據(jù)是 ASP.NET 輔助進(jìn)程的標(biāo)識(或者正在模擬的用戶)的用戶憑據(jù)。在以下示例 ASP.NET 程序中,DefaultCredentials 代表 ASPNET 用戶帳戶(對于在 Microsoft Internet 信息服務(wù) [IIS] 6.0 上運(yùn)行的應(yīng)用程序,則是 NetworkService 用戶帳戶),因?yàn)闆]有對調(diào)用方設(shè)置模擬。

回到頂端

重現(xiàn)此問題的步驟

1.使用 Visual C# .NET 或 Visual Basic .NET 創(chuàng)建新的“ASP.NET Web 服務(wù)”。
2.將該項(xiàng)目命名為“WebServiceTest”。
3.默認(rèn)情況下,會創(chuàng)建“Service1.asmx”。
4.取消默認(rèn) WebMethod“HelloWorld()”的備注標(biāo)記。
5.在“生成”菜單上,單擊“生成解決方案”。
6.關(guān)閉對 WebServiceTest 的匿名訪問。為此,請按照下列步驟操作:
a. 在“控制面板”中,雙擊“管理工具”。
b. 雙擊“Internet 信息服務(wù)”。
c. 展開“Internet 信息服務(wù)”,然后找到“WebServiceTest”虛擬目錄。
d. 右鍵單擊“WebServiceTest”,然后單擊“屬性”。
e. 單擊“目錄安全性”選項(xiàng)卡。
f. 在“匿名訪問和驗(yàn)證控件”下,單擊“編輯”。
g. 在“身份驗(yàn)證方法”對話框中,單擊以清除“匿名訪問”復(fù)選框。
h. 單擊以選中“集成 Windows 驗(yàn)證”復(fù)選框。

注意:確認(rèn)只選中了“集成 Windows 驗(yàn)證”。
i. 單擊“確定”以關(guān)閉“身份驗(yàn)證方法”對話框。
j. 單擊“確定”以關(guān)閉“屬性”。
7.在“生成”菜單上,單擊“生成解決方案”。
8.在瀏覽器中鍵入下面的地址以查看“Service1”Web 服務(wù)說明: http://localhost/WebServiceTest/Service1.asmx
9.測試 HelloWorld WebMethod。WebMethod 能夠如期工作。
10.將一個 Web 引用添加到測試“ASP.NET Web 應(yīng)用程序”。為此,請按照下列步驟操作:
a. 使用 Visual C# .NET 或 Visual Basic .NET 創(chuàng)建新的“ASP.NET Web 應(yīng)用程序”。將該項(xiàng)目命名為“WebServiceCaller”。
b. 默認(rèn)情況下,會創(chuàng)建“WebForm1.aspx”。
c. 在解決方案資源管理器中,右鍵單擊“引用”,然后單擊“添加 Web 引用”。
d. 在“地址”文本框中,鍵入 WebServiceTest 的 URL,如下所示: http://localhost/WebServiceTest/Service1.asmx
e. 單擊“轉(zhuǎn)到”或按 Enter 鍵,然后單擊“添加應(yīng)用”。
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.在“調(diào)試”菜單上,單擊“開始”,然后在瀏覽器中查看應(yīng)用程序。
14.“癥狀”部分討論的錯誤信息將會出現(xiàn)在瀏覽器中。
15.要解決此問題,請將 DefaultCredentials 分配給 Web 服務(wù)客戶端代理的 Credentials 屬性。為此,請在行“Response.Write( myProxy.HelloWorld())”之前插入以下代碼:

Visual C# .NET 示例: myProxy.Credentials=?System.Net.CredentialCache.DefaultCredentials; Visual Basic .NET 示例: myProxy.Credentials?=?System.Net.CredentialCache.DefaultCredentials
16.重復(fù)第 13 步。

轉(zhuǎn)載于:https://www.cnblogs.com/wangtory/archive/2008/03/25/1120895.html

總結(jié)

以上是生活随笔為你收集整理的The request failed with HTTP status 401:Access Denied的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。