WebPermission 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
注意
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使用以下参数集之一调用其构造函数来创建实例:
无参数。 默认 PermissionState 为 None。
PermissionState。 指定允许 Unrestricted 在目标类中使用任何 URI,或 None 仅允许访问通过 AddPermission 该方法指定的 URI。
NetworkAccess值和 URI 字符串。 指定的 URI 具有值授予 NetworkAccess 的权限。
NetworkAccess说明符和 URI 正则表达式。
ConnectList并AcceptList保留你向其授予访问权限的 URI。 若要将 URI 添加到其中任一列表,请使用 AddPermission。 如果作为参数传递Accept,URI 将添加到该参数中AcceptList。NetworkAccess 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 持有的单个接受权限的枚举。 返回枚举中包含的可能对象类型是 String 和 Regex。 |
ConnectList |
此属性返回该 WebPermission 持有的单个连接权限的枚举。 返回枚举中包含的可能对象类型是 String 和 Regex。 |
方法
显式接口实现
IPermission.Demand() |
如果不满足安全要求,则会在运行时引发 SecurityException。 (继承自 CodeAccessPermission) |
IStackWalk.Assert() |
断言调用代码可以访问当前权限对象所标识的资源,即使尚未对堆栈中的高级调用方授予访问该资源的权限。 (继承自 CodeAccessPermission) |
IStackWalk.Demand() |
在运行时确定调用堆栈中的所有调用方是否已被授予当前权限对象所指定的权限。 (继承自 CodeAccessPermission) |
IStackWalk.Deny() |
将导致通过调用代码传递的当前对象的每个 Demand() 失败。 (继承自 CodeAccessPermission) |
IStackWalk.PermitOnly() |
导致所有对象的每个 Demand()(除了通过调用代码的当前那个)失败,即使调用堆栈中较高级别的代码已被授予访问其他资源的权限也是如此。 (继承自 CodeAccessPermission) |