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

示例

以下示例演示如何使用 Regex创建 的新WebPermission实例。 其他主机将添加到 连接和接受 列表 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 中弃用。 如果使用与 CAS 相关的 API,最新版本的 .NET 不会遵循 CAS 注释,并会生成错误。 开发人员应寻求用于完成安全任务的替代方法。

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

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

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

注意

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

注意

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

注意

用户名和默认端口信息是在与提供给构造函数的正则表达式参数进行比较之前从 Uri 中剥离的 WebPermission(NetworkAccess, Regex) 。 如果正则表达式包含用户信息或默认端口号,则所有传入 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 类的两个实例之间的逻辑并集。

适用于

另请参阅