WebPermission 類別

定義

警告

Code Access Security is not supported or honored by the runtime.

控制對 HTTP 網際網路資源存取的權限。

public ref class WebPermission sealed : System::Security::CodeAccessPermission, System::Security::Permissions::IUnrestrictedPermission
public sealed class WebPermission : System.Security.CodeAccessPermission, System.Security.Permissions.IUnrestrictedPermission
[System.Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public sealed class WebPermission : System.Security.CodeAccessPermission, System.Security.Permissions.IUnrestrictedPermission
[System.Serializable]
public sealed class WebPermission : System.Security.CodeAccessPermission, System.Security.Permissions.IUnrestrictedPermission
type WebPermission = class
    inherit CodeAccessPermission
    interface IUnrestrictedPermission
[<System.Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
type WebPermission = class
    inherit CodeAccessPermission
    interface IUnrestrictedPermission
[<System.Serializable>]
type WebPermission = class
    inherit CodeAccessPermission
    interface IUnrestrictedPermission
Public NotInheritable Class WebPermission
Inherits CodeAccessPermission
Implements IUnrestrictedPermission
繼承
屬性
實作

範例

下列範例示範如何使用 建立 的新實例WebPermissionRegex。 其他主機會新增至連線並接受的清單 WebPermission。 最後,連線和接受清單會顯示至主控台。

//  Create a Regex that accepts all URLs containing the host fragment www.contoso.com.
Regex^ myRegex = gcnew Regex( "http://www\\.contoso\\.com/.*" );

// Create a WebPermission that gives permissions to all the hosts containing the same host fragment.
WebPermission^ myWebPermission = gcnew WebPermission( NetworkAccess::Connect,myRegex );

//Add connect privileges for a www.adventure-works.com.
myWebPermission->AddPermission( NetworkAccess::Connect, "http://www.adventure-works.com" );

//Add accept privileges for www.alpineskihouse.com.
myWebPermission->AddPermission( NetworkAccess::Accept, "http://www.alpineskihouse.com/" );

// Check whether all callers higher in the call stack have been granted the permission.
myWebPermission->Demand();

// Get all the URIs with Connect permission.
IEnumerator^ myConnectEnum = myWebPermission->ConnectList;
Console::WriteLine( "\nThe 'URIs' with 'Connect' permission are :\n" );
while ( myConnectEnum->MoveNext() )
{
   Console::WriteLine( "\t{0}", myConnectEnum->Current );
}

// Get all the URIs with Accept permission.   
IEnumerator^ myAcceptEnum = myWebPermission->AcceptList;
Console::WriteLine( "\n\nThe 'URIs' with 'Accept' permission is :\n" );

while ( myAcceptEnum->MoveNext() )
{
   Console::WriteLine( "\t{0}", myAcceptEnum->Current );
}

   //  Create a Regex that accepts all URLs containing the host fragment www.contoso.com.
   Regex myRegex = new Regex(@"http://www\.contoso\.com/.*");

   // Create a WebPermission that gives permissions to all the hosts containing the same host fragment.
   WebPermission myWebPermission = new WebPermission(NetworkAccess.Connect,myRegex);
   
   //Add connect privileges for a www.adventure-works.com.
   myWebPermission.AddPermission(NetworkAccess.Connect,"http://www.adventure-works.com");
   
   //Add accept privileges for www.alpineskihouse.com.
   myWebPermission.AddPermission(NetworkAccess.Accept, "http://www.alpineskihouse.com/");
   
   // Check whether all callers higher in the call stack have been granted the permission.
   myWebPermission.Demand();
   
   // Get all the URIs with Connect permission.
   IEnumerator myConnectEnum = myWebPermission.ConnectList;
   Console.WriteLine("\nThe 'URIs' with 'Connect' permission are :\n");
   while (myConnectEnum.MoveNext())
   {Console.WriteLine("\t" + myConnectEnum.Current);}

   // Get all the URIs with Accept permission.	  
   IEnumerator myAcceptEnum = myWebPermission.AcceptList;
   Console.WriteLine("\n\nThe 'URIs' with 'Accept' permission is :\n");
     
   while (myAcceptEnum.MoveNext())
     {Console.WriteLine("\t" + myAcceptEnum.Current);}
  ' Create a Regex that accepts all the URLs contianing the host fragment www.contoso.com.
  Dim myRegex As New Regex("http://www\.contoso\.com/.*")
    
  ' Create a WebPermission that gives permission to all the hosts containing same host fragment.
  Dim myWebPermission As New WebPermission(NetworkAccess.Connect, myRegex)
  ' Add connect privileges for a www.adventure-works.com.
  myWebPermission.AddPermission(NetworkAccess.Connect, "http://www.adventure-works.com")
  ' Add accept privileges for www.alpineskihouse.com.
  myWebPermission.AddPermission(NetworkAccess.Accept, "http://www.alpineskihouse.com/")
  ' Check whether all callers higher in the call stack have been granted the permission.
  myWebPermission.Demand()
    
  ' Get all the URIs with Connect permission.
  Dim myConnectEnum As IEnumerator = myWebPermission.ConnectList
  Console.WriteLine(ControlChars.NewLine + "The 'URIs' with 'Connect' permission are :" + ControlChars.NewLine)
  While myConnectEnum.MoveNext()
    Console.WriteLine((ControlChars.Tab + myConnectEnum.Current.ToString()))
  End While 
  
  ' Get all the URIs with Accept permission.	  
  Dim myAcceptEnum As IEnumerator = myWebPermission.AcceptList
  Console.WriteLine(ControlChars.NewLine + ControlChars.NewLine + "The 'URIs' with 'Accept' permission is :" + ControlChars.NewLine)

  While myAcceptEnum.MoveNext()
    Console.WriteLine((ControlChars.Tab + myAcceptEnum.Current))
  End While

備註

警告

程式代碼啟用安全性 (CAS) 已淘汰所有版本的 .NET Framework 和 .NET。 最新版本的 .NET 不接受 CAS 批注,並在使用 CAS 相關 API 時產生錯誤。 開發人員應尋求替代方案來完成安全性工作。

WebPermission 提供一組方法和屬性來控制因特網資源的存取。 您可以使用 WebPermission ,根據 PermissionState 建立 時 WebPermission 所設定的 ,提供資源的限制或不受限制的存取。

WebPermission使用下列其中一組參數呼叫其建構函式來建立 實例:

ConnectListAcceptList保存您已授與訪問許可權的 URI。 若要將 URI 新增至其中一個清單,請使用 AddPermission。 如果您傳遞 Accept 做為 NetworkAccess 參數,URI 將會新增至 AcceptListWebPermission 會允許使用符合的 AcceptListURI 連線至目標類別。

警告

若要拒絕存取因特網資源,您必須拒絕存取該資源的所有可能路徑。 這需要呼叫 WebPermission.WebPermission ,並將狀態參數設定為 Deny。 更好的方法是只允許存取特定資源。 如需此主題的詳細資訊,請參閱 使用Deny方法 主題。

注意

您只需要使用資源標準路徑來拒絕存取。 不需要使用所有路徑的語法變化。

注意

使用者名稱和預設埠資訊會在與提供給WebPermission(NetworkAccess, Regex)建構函式的正則表達式自變數比較之前,從 Uri 中移除。 如果正則表達式包含使用者資訊或預設埠號碼,則所有傳入 Uri的 都會無法比對正則表達式。

建構函式

WebPermission()
已淘汰.

建立 WebPermission 類別的新執行個體。

WebPermission(NetworkAccess, Regex)
已淘汰.

以指定 URI 規則運算式的指定存取權限,初始化 WebPermission 類別的新執行個體。

WebPermission(NetworkAccess, String)
已淘汰.

使用指定 URI 的指定存取權限,建立 WebPermission 類別的新執行個體。

WebPermission(PermissionState)
已淘汰.

初始化 WebPermission 類別的新執行個體,這個執行個體可決定是否通過所有要求。

屬性

AcceptList
已淘汰.

這個屬性會傳回由這個 WebPermission 保留的單一接受使用權限其列舉型別。 傳回的列舉型別中所包含的可能物件型別為 StringRegex

ConnectList
已淘汰.

這個屬性會傳回由這個 WebPermission 保留的單一接受使用權限其列舉型別。 傳回的列舉型別中所包含的可能物件型別為 StringRegex

方法

AddPermission(NetworkAccess, Regex)
已淘汰.

以指定的存取權限將指定的 URI 加入至目前 WebPermission

AddPermission(NetworkAccess, String)
已淘汰.

以指定的存取權限將指定的 URI 字串加入至目前 WebPermission

Assert()
已淘汰.

宣告即使堆疊中較高層的呼叫端未獲得資源存取權限,呼叫程式碼仍可透過呼叫這個方法的程式碼要求權限,來存取受保護的資源。 使用 Assert() 會造成安全性問題。

(繼承來源 CodeAccessPermission)
Copy()
已淘汰.

建立 WebPermission 的複本。

Demand()
已淘汰.

如果在呼叫堆疊中較高的所有呼叫端都尚未被授與由目前執行個體所指定之權限,則會在執行階段強制執行 SecurityException

(繼承來源 CodeAccessPermission)
Deny()
已淘汰.
已淘汰.

防止呼叫堆疊中較高的呼叫端,使用程式碼呼叫此方法來存取目前執行個體所指定的資源。

(繼承來源 CodeAccessPermission)
Equals(Object)
已淘汰.

判斷指定的 CodeAccessPermission 物件是否等於目前的 CodeAccessPermission

(繼承來源 CodeAccessPermission)
FromXml(SecurityElement)
已淘汰.

從 XML 編碼方式重建 WebPermission

GetHashCode()
已淘汰.

取得 CodeAccessPermission 物件的雜湊碼,其適合用於雜湊表這類的雜湊演算法和資料結構。

(繼承來源 CodeAccessPermission)
GetType()
已淘汰.

取得目前執行個體的 Type

(繼承來源 Object)
Intersect(IPermission)
已淘汰.

傳回兩個 WebPermission 執行個體的邏輯交集。

IsSubsetOf(IPermission)
已淘汰.

判斷目前 WebPermission 是否為指定物件的子集。

IsUnrestricted()
已淘汰.

檢查 WebPermission 的整體使用權限狀態。

MemberwiseClone()
已淘汰.

建立目前 Object 的淺層複製。

(繼承來源 Object)
PermitOnly()
已淘汰.

防止呼叫堆疊中較高的呼叫端,使用程式碼呼叫此方法來存取目前執行個體所指定之資源以外的所有資源。

(繼承來源 CodeAccessPermission)
ToString()
已淘汰.

建立並傳回目前權限物件的字串表示。

(繼承來源 CodeAccessPermission)
ToXml()
已淘汰.

建立 WebPermission 和其目前狀態的 XML 編碼方式。

Union(IPermission)
已淘汰.

傳回 WebPermission 類別的兩個執行個體間的邏輯等位。

適用於

另請參閱