DnsPermissionAttribute(SecurityAction) 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用指定的 SecurityAction 值初始化 DnsPermissionAttribute 类的新实例。
public:
DnsPermissionAttribute(System::Security::Permissions::SecurityAction action);
public DnsPermissionAttribute (System.Security.Permissions.SecurityAction action);
new System.Net.DnsPermissionAttribute : System.Security.Permissions.SecurityAction -> System.Net.DnsPermissionAttribute
Public Sub New (action As SecurityAction)
参数
- action
- SecurityAction
SecurityAction 值之一。
例外
action
参数不是有效的 SecurityAction。
示例
以下示例使用 DnsPermissionAttribute 将声明性安全性应用于自定义类。
//Uses the DnsPermissionAttribute to restrict access only to those who have permission.
[DnsPermission(SecurityAction::Demand,Unrestricted=true)]
public ref class MyClass
{
public:
static IPAddress^ GetIPAddress()
{
IPAddress^ ipAddress = Dns::Resolve( "localhost" )->AddressList[ 0 ];
return ipAddress;
}
};
int main()
{
try
{
//Grants Access.
Console::WriteLine( " Access granted\n The local host IP Address is :{0}", MyClass::GetIPAddress() );
}
// Denies Access.
catch ( SecurityException^ securityException )
{
Console::WriteLine( "Access denied" );
Console::WriteLine( securityException->ToString() );
}
}
public class MyClass
{
public static IPAddress GetIPAddress()
{
IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
return ipAddress;
}
public static void Main()
{
try
{
//Grants Access.
Console.WriteLine(" Access granted\n The local host IP Address is :" +
MyClass.GetIPAddress().ToString());
}
// Denies Access.
catch (SecurityException securityException)
{
Console.WriteLine("Access denied");
Console.WriteLine(securityException.ToString());
}
}
}
' Uses the DnsPermissionAttribute to restrict access only to those who have permission.
<DnsPermission(SecurityAction.Demand, Unrestricted := true)> _
Public Class MyClass1
Public Shared Function GetIPAddress() As IPAddress
Dim ipAddress As IPAddress = Dns.Resolve("localhost").AddressList(0)
Return ipAddress
End Function 'GetIPAddress
Public Shared Sub Main()
Try
' Grants Access.
Console.WriteLine(("Access granted" + ControlChars.NewLine + " The local host IP Address is :" + MyClass1.GetIPAddress().ToString()))
' Denies Access.
Catch securityException As SecurityException
Console.WriteLine("Access denied")
Console.WriteLine(securityException.ToString())
End Try
End Sub
End Class
注解
SecurityAction传递给此构造函数的值指定允许DnsPermissionAttribute的目标。