Export (0) Print
Expand All

SocketPermissionAttribute Class

Specifies security actions to control Socket connections. This class cannot be inherited.

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

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

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

To use this attribute, your Socket connection must conform to the properties specified in your SocketPermissionAttribute. For example, to apply the permission to a Socket connection on port 80, set the Port property of the SocketPermissionAttribute to "80". The security information specified in SocketPermissionAttribute is stored in the metadata of the attribute target, which is the class to which the SocketPermissionAttribute is applied. The system then accesses the information at run time. The SecurityAction passed to the constructor determines the allowable SocketPermissionAttribute targets.

Note   The properties of a SocketPermissionAttribute must have values that are not a null reference (Nothing in Visual Basic). Also, once set, the values of the properties cannot be changed.
Note   For more information about using attributes, see Extending Metadata Using Attributes.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the SocketPermissionAttribute.

[Visual Basic] 

  ' Show the use of SocketPermission by denying the access to Port 3131 on the localhost.
  <SocketPermission(SecurityAction.Deny, Access := "Connect", Host := "127.0.0.1", Port := "3113", Transport := "All")>  _
  Function GetDate() As [String]
     Dim mysocket As Socket
     
     ' Get the current date from the remote date server listening on port 3114.
     Try
        Dim bytesReceived As Integer
        Dim address As IPAddress
        Dim getByte(100) As Byte
        
        ' Try to connect on port 3113, to show the effect of the access denied defined
        ' by the previous attribute.
        mysocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        
        Try
           address = IPAddress.Parse("127.0.0.1")
           Console.WriteLine("Connecting to localhost, Port = 3113. Access to 3113 is not allowed.")
           mysocket.Connect(New IPEndPoint(address, 3113)) ' This will throw an exception.           
        Catch e As SecurityException
           Console.WriteLine(("SecurityException (expected)" + e.Message))
        End Try
        
        ' Try to connect on port 3114, for which access is allowed. Notice that the
        ' related date server must be listening on port 3114, too.
        mysocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        
        address = IPAddress.Parse("127.0.0.1")
        Console.WriteLine("Connecting to localhost, Port = 3114 (Make sure server is running on this port)")
        
        Console.Write("The current date and time is : ")
        mysocket.Connect(New IPEndPoint(address, 3114)) ' This will not throw an exception.                    
        bytesReceived = mysocket.Receive(getByte, getByte.Length, 0)
        Return asciiEncoding.GetString(getByte, 0, bytesReceived)
     Catch e As Exception
        Console.WriteLine(e.Message)
        Return ""
     End Try
  End Function 'GetDate
 

[C#] 

  // Show the use of SocketPermission by denying the access to Port 3131 on the localhost.
  [SocketPermission(SecurityAction.Deny, Access = "Connect", Host = "127.0.0.1", Port = "3113", Transport = "All")]
   public String GetDate() 
   {
      Socket  mysocket;
      
      // Get the current date from the remote date server listening on port 3114.
      try 
      {
        int bytesReceived;      
        IPAddress address; 
        byte[] getByte = new byte[100];
        
        // Try to connect on port 3113, to show the effect of the access denied defined
        // by the previous attribute.
        mysocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
          
        try 
        {
          address =       IPAddress.Parse("127.0.0.1"); 
          Console.WriteLine("\n\nConnecting to localhost, Port = 3113. Access to 3113 is not allowed.");
          mysocket.Connect(new IPEndPoint(address, 3113)); // This will throw an exception.           
        }
        catch (SecurityException e)
        {
          Console.WriteLine("SecurityException (expected)" + e.Message);
        }
        
        // Try to connect on port 3114, for which access is allowed. Notice that the
        // related date server must be listening on port 3114, too.
        mysocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
          
        address =       IPAddress.Parse("127.0.0.1"); 
        Console.WriteLine("\n\nConnecting to localhost, Port = 3114 (Make sure server is running on this port)");

        Console.Write("The current date and time is : ");
        mysocket.Connect(new IPEndPoint(address, 3114)); // This will not throw an exception.                    
        bytesReceived = mysocket.Receive( getByte, getByte.Length, 0 );
        return asciiEncoding.GetString( getByte, 0, bytesReceived );
      }
      catch(Exception e)
      {
        Console.WriteLine(e.Message);
        return "";
      }
  }
  

[C++] 

  // Show the use of SocketPermission by denying the access to Port 3113 on the localhost.
    [SocketPermission(SecurityAction::Deny, Access = "Connect", Host = "127.0.0.1", Port = "3113", Transport = "All")]
   String *GetDate() 
   {
      Socket  *mysocket;
      
      // Get the current date from the remote date server listening on port 3114.
      try 
      {
        int bytesReceived;      
        IPAddress *address; 
        Byte getByte[] = new Byte[100];
        
        // Try to connect on port 3113, to show the effect of the access denied defined
        // by the previous attribute.
        mysocket = new Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp);
          
        try 
        {
            address =       IPAddress::Parse("127.0.0.1"); 
          Console::WriteLine("\n\nConnecting to localhost, Port = 3113. Access to 3113 is not allowed.");
          mysocket->Connect(new IPEndPoint(address, 3113)); // This will throw an exception.           
        }
        catch (SecurityException *e)
        {
            Console::Write("SecurityException (expected) ");
            Console::WriteLine(e->Message);
        }
        
        // Try to connect on port 3114, for which access is allowed. Notice that the
        // related date server must be listening on port 3114, too.
        mysocket = new Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp);
          
        address =       IPAddress::Parse("127.0.0.1"); 
        Console::WriteLine("\n\nConnecting to localhost, Port = 3114 (Make sure server is running on this port)");

        Console::Write("The current date and time is : ");
        mysocket->Connect(new IPEndPoint(address, 3114)); // This will not throw an exception.                    
        bytesReceived = mysocket->Receive( getByte, 0, getByte->Length, SocketFlags::None );
        return asciiEncoding->GetString( getByte, 0, bytesReceived );  
      }
      catch(Exception *e)
      {
          Console::WriteLine(e->Message);
        return "";
      }
  }
  

[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

SocketPermissionAttribute Members | System.Net Namespace | Declarative Security | Imperative Security | SocketPermission

Show:
© 2014 Microsoft