次の方法で共有


SocketPermissionAttribute クラス

Socket 接続を制御するセキュリティ アクションを指定します。このクラスは継承できません。

この型のすべてのメンバの一覧については、SocketPermissionAttribute メンバ を参照してください。

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

<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

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

この属性を使用するには、 Socket 接続が、 SocketPermissionAttribute で指定したプロパティに準拠している必要があります。たとえば、ポート 80 の Socket 接続にアクセス許可を適用するには、 SocketPermissionAttributePort プロパティを "80" に設定します。 SocketPermissionAttribute で指定されたセキュリティ情報は、 SocketPermissionAttribute が適用されるクラスである属性ターゲットのメタデータに格納されます。その後、システムは、実行時にその情報にアクセスします。コンストラクタに渡される SecurityAction は、許容 SocketPermissionAttribute ターゲットを判断します。

メモ    SocketPermissionAttribute のプロパティは、 null 参照 (Visual Basic では Nothing) 以外の値に設定する必要があります。また、プロパティの値は一度設定したら、変更できません。

メモ   属性の使用方法については、「 属性を使用したメタデータの拡張 」を参照してください。

使用例

[Visual Basic, C#, C++] SocketPermissionAttribute を使用する方法を次の例に示します。

 

  ' 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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Net

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System (System.dll 内)

参照

SocketPermissionAttribute メンバ | System.Net 名前空間 | 宣言セキュリティ | 強制セキュリティ | SocketPermission