WebPermission 类

定义

注意

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

控制访问 HTTP Internet 资源的权限。

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
继承
属性
实现

示例

以下示例演示如何创建新的 WebPermission 使用实例 Regex。 将其他主机添加到连接并接受列表 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

注解

WebPermission 提供一组用于控制对 Internet 资源的访问的方法和属性。 可以使用 a, WebPermission 根据 PermissionState 创建资源时 WebPermission 设置的资源提供受限或不受限制的访问。

WebPermission使用以下参数集之一调用其构造函数来创建实例:

ConnectListAcceptList保留你向其授予访问权限的 URI。 若要将 URI 添加到其中任一列表,请使用 AddPermission。 如果作为参数传递Accept,URI 将添加到该参数中AcceptListNetworkAccess WebPermission 将允许与目标类建立连接,其中 URI 匹配 AcceptList

注意

若要拒绝访问 Internet 资源,必须拒绝对该资源的所有可能路径的访问。 这需要调用 WebPermission.WebPermission 状态参数设置为 Deny。 更好的方法是仅允许访问特定资源。 有关此主题的详细信息,请参阅 “使用拒绝方法” 主题。

备注

只需使用资源规范路径拒绝访问。 无需使用所有路径的语法变体。

备注

用户名和默认端口信息在与提供给WebPermission(NetworkAccess, Regex)构造函数的正则表达式参数进行比较之前会从Uri中删除。 如果正则表达式包含用户信息或默认端口号,则所有传入 Uri的 s 将不匹配正则表达式。

构造函数

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 类的两个实例之间的逻辑并集。

显式接口实现

IPermission.Demand()

如果不满足安全要求,则会在运行时引发 SecurityException

(继承自 CodeAccessPermission)
IStackWalk.Assert()

断言调用代码可以访问当前权限对象所标识的资源,即使尚未对堆栈中的高级调用方授予访问该资源的权限。

(继承自 CodeAccessPermission)
IStackWalk.Demand()

在运行时确定调用堆栈中的所有调用方是否已被授予当前权限对象所指定的权限。

(继承自 CodeAccessPermission)
IStackWalk.Deny()

将导致通过调用代码传递的当前对象的每个 Demand() 失败。

(继承自 CodeAccessPermission)
IStackWalk.PermitOnly()

导致所有对象的每个 Demand()(除了通过调用代码的当前那个)失败,即使调用堆栈中较高级别的代码已被授予访问其他资源的权限也是如此。

(继承自 CodeAccessPermission)

适用于

另请参阅