Поделиться через


DnsPermissionAttribute(SecurityAction) Конструктор

Определение

Инициализирует новый экземпляр класса DnsPermissionAttribute заданным значением SecurityAction.

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 объекты.

Применяется к

См. также раздел