WebPermission 类

定义

控制访问 HTTP Internet 资源的权限。Controls rights to access HTTP Internet resources.

public ref class WebPermission sealed : 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
Public NotInheritable Class WebPermission
Inherits CodeAccessPermission
Implements IUnrestrictedPermission
继承
属性
实现

示例

下面的示例演示如何使用 Regex创建 WebPermission 的新实例。The following example demonstrates how to create a new instance of WebPermission using a Regex. 其他主机将添加到 WebPermission的 "连接" 和 "接受" 列表中。Additional hosts are added to the connect and accept list of WebPermission. 最后,将 "连接" 和 "接受" 列表显示到控制台。Finally, the connect and accept list are displayed to the console.

//  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 资源的访问。WebPermission provides a set of methods and properties to control access to Internet resources. 你可以使用 WebPermission 基于创建 WebPermission 时设置的 PermissionState 提供对资源的受限访问或无限制访问。You can use a WebPermission to provide either restricted or unrestricted access to your resource, based on the PermissionState that is set when the WebPermission is created.

通过使用以下参数集中的一组调用其构造函数来创建 WebPermission 实例:Create a WebPermission instance by calling its constructor using one of the following sets of parameters:

ConnectListAcceptList 保存已向其授予访问权限的 Uri。The ConnectList and AcceptList hold the URIs to which you have granted access permission. 若要将 URI 添加到其中一个列表,请使用 AddPermissionTo add a URI to either of these lists, use AddPermission. 如果将 Accept 作为 NetworkAccess 参数传递,则 URI 将添加到 AcceptList中。If you pass Accept as the NetworkAccess parameter, the URI will be added to the AcceptList. WebPermission 将允许与与 AcceptList匹配的 Uri 连接到目标类。WebPermission will allow connections to your target class with URIs matching the AcceptList.

注意

若要拒绝对 Internet 资源的访问,必须拒绝对该资源的所有可能路径的访问。To deny access to an Internet resource, you must deny access to all the possible paths to that resource. 这需要调用状态参数设置为 DenyWebPermission.WebPermissionThis requires calling WebPermission.WebPermission with state parameter set to Deny. 更好的方法是只允许访问特定资源。A better approach is to allow access to the specific resource only. 有关此主题的详细信息,请参阅Using The Deny 方法主题。For more information about this subject, refer to the Using the Deny Method topic.

备注

只需使用资源规范路径,拒绝访问。You need to deny access using only the resource canonical path. 无需使用路径的所有语法变化形式。There is no need to use all the path's syntactical variations.

备注

在与提供给 WebPermission(NetworkAccess, Regex) 构造函数的正则表达式自变量进行比较之前,将从 Uri 中去除用户名和默认端口信息。User name and default port information is stripped from the Uri before the comparison with the regular expression argument that is supplied to the WebPermission(NetworkAccess, Regex) constructor. 如果正则表达式包含用户信息或默认端口号,则所有传入的 Uri将无法与正则表达式匹配。If the regular expression contains user information or the default port number, then all incoming Uris will fail to match the regular expression.

构造函数

WebPermission()

创建 WebPermission 类的新实例。Creates a new instance of the WebPermission class.

WebPermission(NetworkAccess, Regex)

使用指定 URI 正则表达式的指定访问权限初始化 WebPermission 类的新实例。Initializes a new instance of the WebPermission class with the specified access rights for the specified URI regular expression.

WebPermission(NetworkAccess, String)

使用指定 URI 的指定访问权限初始化 WebPermission 类的新实例。Initializes a new instance of the WebPermission class with the specified access rights for the specified URI.

WebPermission(PermissionState)

创建 WebPermission 类的新实例,该实例允许所有命令或禁止所有命令。Creates a new instance of the WebPermission class that passes all demands or fails all demands.

属性

AcceptList

此属性返回该 WebPermission 持有的单个接受权限的枚举。This property returns an enumeration of a single accept permissions held by this WebPermission. 返回枚举中包含的可能对象类型是 StringRegexThe possible objects types contained in the returned enumeration are String and Regex.

ConnectList

此属性返回该 WebPermission 持有的单个连接权限的枚举。This property returns an enumeration of a single connect permissions held by this WebPermission. 返回枚举中包含的可能对象类型是 StringRegexThe possible objects types contained in the returned enumeration are String and Regex.

方法

AddPermission(NetworkAccess, Regex)

将具有指定访问权限的指定 URI 添加到当前 WebPermissionAdds the specified URI with the specified access rights to the current WebPermission.

AddPermission(NetworkAccess, String)

将具有指定访问权限的指定 URI 字符串添加到当前 WebPermissionAdds the specified URI string with the specified access rights to the current WebPermission.

Assert()

声明调用代码能够通过调用此方法的代码,访问受权限请求保护的资源,即使未对堆栈中处于较高位置的调用方授予访问该资源的权限。Declares that the calling code can access the resource protected by a permission demand through the code that calls this method, even if callers higher in the stack have not been granted permission to access the resource. 使用 Assert() 会引起安全问题。Using Assert() can create security issues.

(继承自 CodeAccessPermission)
Copy()

创建 WebPermission 的副本。Creates a copy of a WebPermission.

Demand()

如果未对调用堆栈中处于较高位置的所有调用方授予当前实例所指定的权限,则在运行时强制 SecurityExceptionForces a SecurityException at run time if all callers higher in the call stack have not been granted the permission specified by the current instance.

(继承自 CodeAccessPermission)
Deny()

防止处于调用堆栈较高位置的调用函数使用可以调用此方法来访问当前实例指定资源的代码。Prevents callers higher in the call stack from using the code that calls this method to access the resource specified by the current instance.

(继承自 CodeAccessPermission)
Equals(Object)

确定指定的 CodeAccessPermission 对象是否等于当前的 CodeAccessPermissionDetermines whether the specified CodeAccessPermission object is equal to the current CodeAccessPermission.

(继承自 CodeAccessPermission)
FromXml(SecurityElement)

通过 XML 编码重新构造 WebPermissionReconstructs a WebPermission from an XML encoding.

GetHashCode()

获取 CodeAccessPermission 对象的哈希代码,此代码适合在哈希算法和数据结构(例如哈希表)中使用。Gets a hash code for the CodeAccessPermission object that is suitable for use in hashing algorithms and data structures such as a hash table.

(继承自 CodeAccessPermission)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
Intersect(IPermission)

返回两个 WebPermission 实例的逻辑交集。Returns the logical intersection of two WebPermission instances.

IsSubsetOf(IPermission)

确定当前 WebPermission 是否是指定对象的子集。Determines whether the current WebPermission is a subset of the specified object.

IsUnrestricted()

检查 WebPermission 的整体权限状态。Checks the overall permission state of the WebPermission.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
PermitOnly()

防止处于调用堆栈较高位置的调用函数使用此代码,此代码将调用此方法来访问除当前实例指定的资源以外的所有资源。Prevents callers higher in the call stack from using the code that calls this method to access all resources except for the resource specified by the current instance.

(继承自 CodeAccessPermission)
ToString()

创建并返回当前权限对象的字符串表示形式。Creates and returns a string representation of the current permission object.

(继承自 CodeAccessPermission)
ToXml()

创建 WebPermission 及其当前状态的 XML 编码。Creates an XML encoding of a WebPermission and its current state.

Union(IPermission)

返回 WebPermission 类的两个实例之间的逻辑并集。Returns the logical union between two instances of the WebPermission class.

显式界面实现

IPermission.Demand() (继承自 CodeAccessPermission)
IStackWalk.Assert() (继承自 CodeAccessPermission)
IStackWalk.Demand() (继承自 CodeAccessPermission)
IStackWalk.Deny() (继承自 CodeAccessPermission)
IStackWalk.PermitOnly() (继承自 CodeAccessPermission)

适用于

另请参阅