Export (0) Print
Expand All

DnsPermissionAttribute Class

Specifies permission to request information from Domain Name Servers.

For a list of all members of this type, see DnsPermissionAttribute Members.

System.Object
   System.Attribute
      System.Security.Permissions.SecurityAttribute
         System.Security.Permissions.CodeAccessSecurityAttribute
            System.Net.DnsPermissionAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Assembly Or AttributeTargets.Class _
   Or AttributeTargets.Struct Or AttributeTargets.Constructor Or _
   AttributeTargets.Method)>
<Serializable>
NotInheritable Public Class DnsPermissionAttribute
   Inherits CodeAccessSecurityAttribute
[C#]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class
   | AttributeTargets.Struct | AttributeTargets.Constructor |
   AttributeTargets.Method)]
[Serializable]
public sealed class DnsPermissionAttribute :
   CodeAccessSecurityAttribute
[C++]
[AttributeUsage(AttributeTargets::Assembly |
   AttributeTargets::Class | AttributeTargets::Struct |
   AttributeTargets::Constructor | AttributeTargets::Method)]
[Serializable]
public __gc __sealed class DnsPermissionAttribute : public
   CodeAccessSecurityAttribute
[JScript]
public
   AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class |
   AttributeTargets.Struct | AttributeTargets.Constructor |
   AttributeTargets.Method)
 Serializable
class DnsPermissionAttribute extends
   CodeAccessSecurityAttribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The security information declared by DnsPermissionAttribute is stored in the metadata of the attribute target, which is the class to which the DnsPermissionAttribute is applied. The system then accesses this information at run-time. The SecurityAction passed to the constructor determines the allowable DNS targets.

These security attributes are used only for Declarative Security. For Imperative Security, use the corresponding DnsPermission class.

Security access is either fully restricted, or fully unrestricted. Set the Unrestricted property to true to grant access, or false for no access. Set this property as a named parameter.

For more information about using attributes, see Extending Metadata Using Attributes.

Example

[Visual Basic, C#, C++] The following example uses DnsPermissionAttribute to apply declarative security to a custom class.

[Visual Basic] 
' 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

[C#] 
//Uses the DnsPermissionAttribute to restrict access only to those who have permission.
[DnsPermission(SecurityAction.Demand, Unrestricted = true)]
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());
}

[C++] 
//Uses the DnsPermissionAttribute to restrict access only to those who have permission.
[DnsPermission(SecurityAction::Demand, Unrestricted = true)]
public __gc class MyClass{
public:
    static IPAddress* GetIPAddress(){
        IPAddress* ipAddress = Dns::Resolve(S"localhost")->AddressList[0];
        return ipAddress;
    }
};

int main(){
    try{
        //Grants Access.
        Console::WriteLine(S" Access granted\n The local host IP Address is :{0}", MyClass::GetIPAddress());
    }
    // Denies Access.
    catch(SecurityException* securityException){
        Console::WriteLine(S"Access denied");    
        Console::WriteLine(securityException->ToString());
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Net

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

DnsPermissionAttribute Members | System.Net Namespace | CodeAccessSecurityAttribute

Show:
© 2014 Microsoft