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的目標。