SslStream コンストラクター

定義

SslStream クラスの新しいインスタンスを初期化します。

オーバーロード

SslStream(Stream)

指定した Stream を使用して SslStream クラスの新しいインスタンスを初期化します。

SslStream(Stream, Boolean)

指定した Stream とストリームを閉じる動作を使用して、SslStream クラスの新しいインスタンスを初期化します。

SslStream(Stream, Boolean, RemoteCertificateValidationCallback)

指定した Stream、ストリームを閉じる動作、および証明書検証デリゲートを使用して、SslStream クラスの新しいインスタンスを初期化します。

SslStream(Stream, Boolean, RemoteCertificateValidationCallback, LocalCertificateSelectionCallback)

指定した Stream、ストリームを閉じる動作、証明書検証デリゲート、および証明書選択デリゲートを使用して、SslStream クラスの新しいインスタンスを初期化します。

SslStream(Stream, Boolean, RemoteCertificateValidationCallback, LocalCertificateSelectionCallback, EncryptionPolicy)

指定した Stream を使用して SslStream クラスの新しいインスタンスを初期化します。

注釈

指定した SslStream ストリームを閉じないようにするには、コンストラクターを SslStream 使用します。

SslStream(Stream)

指定した Stream を使用して SslStream クラスの新しいインスタンスを初期化します。

public:
 SslStream(System::IO::Stream ^ innerStream);
public SslStream (System.IO.Stream innerStream);
new System.Net.Security.SslStream : System.IO.Stream -> System.Net.Security.SslStream
Public Sub New (innerStream As Stream)

パラメーター

innerStream
Stream

SslStream がデータの送受信に使用する Stream オブジェクト。

例外

innerStream が読み取り可能ではありません。

または innerStream が書き込み可能ではありません。

innerStreamnullです。

または innerStreamNull が等価です。

注釈

encryptionpolicy の構成ファイルに値が指定されていない場合は、EncryptionPolicy構築されるインスタンスのSslStream既定値EncryptionPolicy.RequireEncryptionが使用されます。

暗号化ポリシーが に設定されている場合は、Null 暗号を使用する EncryptionPolicy.NoEncryption必要があります。

適用対象

SslStream(Stream, Boolean)

指定した Stream とストリームを閉じる動作を使用して、SslStream クラスの新しいインスタンスを初期化します。

public:
 SslStream(System::IO::Stream ^ innerStream, bool leaveInnerStreamOpen);
public SslStream (System.IO.Stream innerStream, bool leaveInnerStreamOpen);
new System.Net.Security.SslStream : System.IO.Stream * bool -> System.Net.Security.SslStream
Public Sub New (innerStream As Stream, leaveInnerStreamOpen As Boolean)

パラメーター

innerStream
Stream

SslStream がデータの送受信に使用する Stream オブジェクト。

leaveInnerStreamOpen
Boolean

SslStream がデータの送受信に使用する Stream オブジェクトの閉じる動作を示すブール値。 このパラメーターは、内部ストリームを開いたままにするかどうかを示します。

例外

innerStream が読み取り可能ではありません。

または innerStream が書き込み可能ではありません。

innerStreamnullです。

または innerStreamNull が等価です。

次のコード例では、このコンストラクターの呼び出しを示します。

static void ProcessClient( TcpClient^ client )
{
   
   // A client has connected. Create the 
   // SslStream using the client's network stream.
   SslStream^ sslStream = gcnew SslStream( client->GetStream(),false );
   
   // Authenticate the server but don't require the client to authenticate.
   try
   {
      sslStream->AuthenticateAsServer( serverCertificate, false, true );
      // false == no client cert required; true == check cert revocation.
      
      // Display the properties and settings for the authenticated stream.
      DisplaySecurityLevel( sslStream );
      DisplaySecurityServices( sslStream );
      DisplayCertificateInformation( sslStream );
      DisplayStreamProperties( sslStream );
      
      // Set timeouts for the read and write to 5 seconds.
      sslStream->ReadTimeout = 5000;
      sslStream->WriteTimeout = 5000;
      
      // Read a message from the client.   
      Console::WriteLine( L"Waiting for client message..." );
      String^ messageData = ReadMessage( sslStream );
      Console::WriteLine( L"Received: {0}", messageData );
      
      // Write a message to the client.
      array<Byte>^message = Encoding::UTF8->GetBytes( L"Hello from the server.<EOF>" );
      Console::WriteLine( L"Sending hello message." );
      sslStream->Write( message );
   }
   catch ( AuthenticationException^ e ) 
   {
      Console::WriteLine( L"Exception: {0}", e->Message );
      if ( e->InnerException != nullptr )
      {
         Console::WriteLine( L"Inner exception: {0}", e->InnerException->Message );
      }
      Console::WriteLine( L"Authentication failed - closing the connection." );
      sslStream->Close();
      client->Close();
      return;
   }
   finally
   {
      
      // The client stream will be closed with the sslStream
      // because we specified this behavior when creating
      // the sslStream.
      sslStream->Close();
      client->Close();
   }

}
static void ProcessClient (TcpClient client)
{
    // A client has connected. Create the
    // SslStream using the client's network stream.
    SslStream sslStream = new SslStream(
        client.GetStream(), false);
    // Authenticate the server but don't require the client to authenticate.
    try
    {
        sslStream.AuthenticateAsServer(serverCertificate, clientCertificateRequired: false, checkCertificateRevocation: true);

        // Display the properties and settings for the authenticated stream.
        DisplaySecurityLevel(sslStream);
        DisplaySecurityServices(sslStream);
        DisplayCertificateInformation(sslStream);
        DisplayStreamProperties(sslStream);

        // Set timeouts for the read and write to 5 seconds.
        sslStream.ReadTimeout = 5000;
        sslStream.WriteTimeout = 5000;
        // Read a message from the client.
        Console.WriteLine("Waiting for client message...");
        string messageData = ReadMessage(sslStream);
        Console.WriteLine("Received: {0}", messageData);

        // Write a message to the client.
        byte[] message = Encoding.UTF8.GetBytes("Hello from the server.<EOF>");
        Console.WriteLine("Sending hello message.");
        sslStream.Write(message);
    }
    catch (AuthenticationException e)
    {
        Console.WriteLine("Exception: {0}", e.Message);
        if (e.InnerException != null)
        {
            Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
        }
        Console.WriteLine ("Authentication failed - closing the connection.");
        sslStream.Close();
        client.Close();
        return;
    }
    finally
    {
        // The client stream will be closed with the sslStream
        // because we specified this behavior when creating
        // the sslStream.
        sslStream.Close();
        client.Close();
    }
}
Private Shared Sub ProcessClient(client As TcpClient)
    ' A client has connected. Create the 
    ' SslStream using the client's network stream.
    Dim sslStream = New SslStream(client.GetStream(), False)

    Try

        sslStream.AuthenticateAsServer(serverCertificate, clientCertificateRequired:=False, checkCertificateRevocation:=True)
        ' Display the properties And settings for the authenticated stream.
        DisplaySecurityLevel(sslStream)
        DisplaySecurityServices(sslStream)
        DisplayCertificateInformation(sslStream)
        DisplayStreamProperties(sslStream)

        ' Set timeouts for the read and write to 5 seconds.
        sslStream.ReadTimeout = 5000
        sslStream.WriteTimeout = 5000

        ' Read a message from the client.   
        Console.WriteLine("Waiting for client message...")
        Dim messageData As String = ReadMessage(sslStream)
        Console.WriteLine("Received: {0}", messageData)

        ' Write a message to the client.
        Dim message As Byte() = Encoding.UTF8.GetBytes("Hello from the server.<EOF>")
        Console.WriteLine("Sending hello message.")
        sslStream.Write(message)
    Catch e As AuthenticationException
        Console.WriteLine("Exception: {0}", e.Message)

        If e.InnerException IsNot Nothing Then
            Console.WriteLine("Inner exception: {0}", e.InnerException.Message)
        End If

        Console.WriteLine("Authentication failed - closing the connection.")
        sslStream.Close()
        client.Close()
        Return
    Finally
        ' The client stream will be closed with the sslStream
        ' because we specified this behavior when creating
        ' the sslStream.
        sslStream.Close()
        client.Close()
    End Try
End Sub

注釈

パラメーターにleaveStreamOpen指定trueした場合、閉じるSslStreamとストリームに影響innerStreamはありません。不要になったら明示的に閉じるinnerStream必要があります。

encryptionpolicy の構成ファイルに値が指定されていない場合は、EncryptionPolicy構築されるインスタンスのSslStream既定値EncryptionPolicy.RequireEncryptionが使用されます。

暗号化ポリシーが に設定されている場合は、Null 暗号を使用する EncryptionPolicy.NoEncryption必要があります。

適用対象

SslStream(Stream, Boolean, RemoteCertificateValidationCallback)

指定した Stream、ストリームを閉じる動作、および証明書検証デリゲートを使用して、SslStream クラスの新しいインスタンスを初期化します。

public:
 SslStream(System::IO::Stream ^ innerStream, bool leaveInnerStreamOpen, System::Net::Security::RemoteCertificateValidationCallback ^ userCertificateValidationCallback);
public SslStream (System.IO.Stream innerStream, bool leaveInnerStreamOpen, System.Net.Security.RemoteCertificateValidationCallback? userCertificateValidationCallback);
public SslStream (System.IO.Stream innerStream, bool leaveInnerStreamOpen, System.Net.Security.RemoteCertificateValidationCallback userCertificateValidationCallback);
new System.Net.Security.SslStream : System.IO.Stream * bool * System.Net.Security.RemoteCertificateValidationCallback -> System.Net.Security.SslStream
Public Sub New (innerStream As Stream, leaveInnerStreamOpen As Boolean, userCertificateValidationCallback As RemoteCertificateValidationCallback)

パラメーター

innerStream
Stream

SslStream がデータの送受信に使用する Stream オブジェクト。

leaveInnerStreamOpen
Boolean

SslStream がデータの送受信に使用する Stream オブジェクトの閉じる動作を示すブール値。 このパラメーターは、内部ストリームを開いたままにするかどうかを示します。

userCertificateValidationCallback
RemoteCertificateValidationCallback

リモート側によって提供される証明書を検証する RemoteCertificateValidationCallback デリゲート。

例外

innerStream が読み取り可能ではありません。

または innerStream が書き込み可能ではありません。

innerStreamnullです。

または innerStreamNull が等価です。

次のコード例では、認証のクライアント部分を SslStream 作成して開始します。

// Create a TCP/IP client socket.
// machineName is the host running the server application.
TcpClient^ client = gcnew TcpClient(machineName, 8080);
Console::WriteLine("Client connected.");
  
// Create an SSL stream that will close 
// the client's stream.
SslStream^ sslStream = gcnew SslStream(
    client->GetStream(), false,
    gcnew RemoteCertificateValidationCallback(ValidateServerCertificate),
    nullptr);
  
// The server name must match the name
// on the server certificate.
try
{
    sslStream->AuthenticateAsClient(serverName);
}
catch (AuthenticationException^ ex) 
{
    Console::WriteLine("Exception: {0}", ex->Message);
    if (ex->InnerException != nullptr)
    {
        Console::WriteLine("Inner exception: {0}", 
            ex->InnerException->Message);
    }

    Console::WriteLine("Authentication failed - "
        "closing the connection.");
    sslStream->Close();
    client->Close();
    return;
}
// Create a TCP/IP client socket.
// machineName is the host running the server application.
TcpClient client = new TcpClient(machineName,443);
Console.WriteLine("Client connected.");
// Create an SSL stream that will close the client's stream.
SslStream sslStream = new SslStream(
    client.GetStream(),
    false,
    new RemoteCertificateValidationCallback (ValidateServerCertificate),
    null
    );
// The server name must match the name on the server certificate.
try
{
    sslStream.AuthenticateAsClient(serverName);
}
catch (AuthenticationException e)
{
    Console.WriteLine("Exception: {0}", e.Message);
    if (e.InnerException != null)
    {
        Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
    }
    Console.WriteLine ("Authentication failed - closing the connection.");
    client.Close();
    return;
}
' Create a TCP/IP client socket.
' machineName is the host running the server application.
Dim client = New TcpClient(machineName, 443)
Console.WriteLine("Client connected.")

' Create an SSL stream that will close the client's stream.
Dim sslStream = New SslStream(
    client.GetStream(), False, 
    New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate), Nothing)

' The server name must match the name on the server certificate.
Try
    sslStream.AuthenticateAsClient(serverName)
Catch e As AuthenticationException
    Console.WriteLine("Exception: {0}", e.Message)

    If e.InnerException IsNot Nothing Then
        Console.WriteLine("Inner exception: {0}", e.InnerException.Message)
    End If

    Console.WriteLine("Authentication failed - closing the connection.")
    client.Close()
    Return
End Try

注釈

パラメーターにleaveStreamOpen指定trueした場合、閉じるSslStreamとストリームに影響innerStreamはありません。不要になったら明示的に閉じるinnerStream必要があります。

userCertificateValidationCallbackデリゲートのcertificateErrors引数には、チャネル セキュリティ サポート プロバイダー インターフェイス (SSPI) によって返されるWindowsエラー コードが含まれています。 デリゲートによって呼び出されたメソッドの戻り値によって userCertificateValidationCallback 、認証が成功するかどうかが決まります。

セキュリティ プロトコルと暗号化アルゴリズムは、デリゲートのメソッドが呼び出されたときに既に userCertificateValidationCallback 選択されています。 この方法を使用して、選択した暗号化アルゴリズムと強度がアプリケーションに十分かどうかを判断できます。 そうでない場合は、メソッドが作成されないようにSslStream戻るfalse必要があります。

encryptionpolicy の構成ファイルに値が指定されていない場合は、EncryptionPolicy構築されるインスタンスのSslStream既定値EncryptionPolicy.RequireEncryptionが使用されます。

暗号化ポリシーが に設定されている場合は、Null 暗号を使用する EncryptionPolicy.NoEncryption必要があります。

注意

フレームワークは、作成された SSL セッションをキャッシュし、可能な場合は、キャッシュされたセッションを新しい要求に再利用しようとします。 SSL セッションを再利用しようとすると、フレームワークは最初の ClientCertificates 要素 (存在する場合) を使用するか、空の場合 ClientCertificates は匿名セッションを再利用しようとします。

注意

クライアント証明書は、SSL バージョン 2 プロトコルではサポートされていません。

適用対象

SslStream(Stream, Boolean, RemoteCertificateValidationCallback, LocalCertificateSelectionCallback)

指定した Stream、ストリームを閉じる動作、証明書検証デリゲート、および証明書選択デリゲートを使用して、SslStream クラスの新しいインスタンスを初期化します。

public:
 SslStream(System::IO::Stream ^ innerStream, bool leaveInnerStreamOpen, System::Net::Security::RemoteCertificateValidationCallback ^ userCertificateValidationCallback, System::Net::Security::LocalCertificateSelectionCallback ^ userCertificateSelectionCallback);
public SslStream (System.IO.Stream innerStream, bool leaveInnerStreamOpen, System.Net.Security.RemoteCertificateValidationCallback? userCertificateValidationCallback, System.Net.Security.LocalCertificateSelectionCallback? userCertificateSelectionCallback);
public SslStream (System.IO.Stream innerStream, bool leaveInnerStreamOpen, System.Net.Security.RemoteCertificateValidationCallback userCertificateValidationCallback, System.Net.Security.LocalCertificateSelectionCallback userCertificateSelectionCallback);
new System.Net.Security.SslStream : System.IO.Stream * bool * System.Net.Security.RemoteCertificateValidationCallback * System.Net.Security.LocalCertificateSelectionCallback -> System.Net.Security.SslStream
Public Sub New (innerStream As Stream, leaveInnerStreamOpen As Boolean, userCertificateValidationCallback As RemoteCertificateValidationCallback, userCertificateSelectionCallback As LocalCertificateSelectionCallback)

パラメーター

innerStream
Stream

SslStream がデータの送受信に使用する Stream オブジェクト。

leaveInnerStreamOpen
Boolean

SslStream がデータの送受信に使用する Stream オブジェクトの閉じる動作を示すブール値。 このパラメーターは、内部ストリームを開いたままにするかどうかを示します。

userCertificateValidationCallback
RemoteCertificateValidationCallback

リモート側によって提供される証明書を検証する RemoteCertificateValidationCallback デリゲート。

userCertificateSelectionCallback
LocalCertificateSelectionCallback

認証に使用する証明書を選択する LocalCertificateSelectionCallback デリゲート。

例外

innerStream が読み取り可能ではありません。

または innerStream が書き込み可能ではありません。

innerStreamnullです。

または innerStreamNull が等価です。

次のコード例では、このコンストラクターの呼び出しを示します。 この例は、クラスに提供されるより大きな例の SslStream 一部です。

// Server name must match the host name and the name on the host's certificate. 
serverName = args[ 1 ];

// Create a TCP/IP client socket.
TcpClient^ client = gcnew TcpClient( serverName,80 );
Console::WriteLine( L"Client connected." );

// Create an SSL stream that will close the client's stream.
SslStream^ sslStream = gcnew SslStream( 
    client->GetStream(),
    false,
    gcnew RemoteCertificateValidationCallback( ValidateServerCertificate ),
    gcnew LocalCertificateSelectionCallback( SelectLocalCertificate ) );
// Server name must match the host name and the name on the host's certificate.
serverName = args[0];
// Create a TCP/IP client socket.
TcpClient client = new TcpClient(serverName,80);
Console.WriteLine("Client connected.");
// Create an SSL stream that will close the client's stream.
SslStream sslStream = new SslStream(
    client.GetStream(),
    false,
    new RemoteCertificateValidationCallback (ValidateServerCertificate),
    new LocalCertificateSelectionCallback(SelectLocalCertificate)
    );
' Server name must match the host name and the name on the host's certificate. 
serverName = args(0)
' Create a TCP/IP client socket.
Dim client As New TcpClient(serverName, 80)
Console.WriteLine("Client connected.")
' Create an SSL stream that will close the client's stream.
Dim sslStream As New SslStream(
    client.GetStream(), False, 
    New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate), 
    New LocalCertificateSelectionCallback(AddressOf SelectLocalCertificate))

注釈

パラメーターにleaveStreamOpen指定trueした場合、閉じるSslStreamとストリームに影響innerStreamはありません。不要になった場合は、明示的に閉じるinnerStream必要があります。

userCertificateValidationCallbackデリゲートのcertificateErrors引数には、チャネル セキュリティ サポート プロバイダー インターフェイス (SSPI) によって返されるWindowsエラー コードが含まれています。 デリゲートによって呼び出されたメソッドの戻り値によって userCertificateValidationCallback 、認証が成功するかどうかが決まります。

デリゲートのメソッドが呼び出されると、セキュリティ プロトコルと暗号化アルゴリズムが既に userCertificateValidationCallback 選択されています。 このメソッドを使用して、選択した暗号化アルゴリズムと強度がアプリケーションに十分かどうかを判断できます。 そうでない場合は、メソッドが作成されないようにSslStream戻るfalse必要があります。

userCertificateSelectionCallbackこのデリゲートは、アプリケーションに複数の証明書があり、証明書を動的に選択する必要がある場合に便利です。 "MY" ストア内の証明書は、デリゲートによって呼び出されたメソッドに渡されます。

encryptionpolicy の構成ファイルに値が指定されていない場合は、EncryptionPolicy構築されるインスタンスのSslStream既定値EncryptionPolicy.RequireEncryptionになります。

暗号化ポリシーが に設定されている場合は、Null 暗号を使用する EncryptionPolicy.NoEncryption必要があります。

注意

フレームワークは、作成時に SSL セッションをキャッシュし、可能であれば、キャッシュされたセッションを新しい要求に再利用しようとします。 SSL セッションを再利用しようとすると、フレームワークは P:System.Net.HttpWebRequest.ClientCertificates の最初の要素 (存在する場合) を使用するか、P:System.Net.HttpWebRequest.ClientCertificates が空の場合に匿名セッションを再利用しようとします。

適用対象

SslStream(Stream, Boolean, RemoteCertificateValidationCallback, LocalCertificateSelectionCallback, EncryptionPolicy)

指定した Stream を使用して SslStream クラスの新しいインスタンスを初期化します。

public:
 SslStream(System::IO::Stream ^ innerStream, bool leaveInnerStreamOpen, System::Net::Security::RemoteCertificateValidationCallback ^ userCertificateValidationCallback, System::Net::Security::LocalCertificateSelectionCallback ^ userCertificateSelectionCallback, System::Net::Security::EncryptionPolicy encryptionPolicy);
public SslStream (System.IO.Stream innerStream, bool leaveInnerStreamOpen, System.Net.Security.RemoteCertificateValidationCallback? userCertificateValidationCallback, System.Net.Security.LocalCertificateSelectionCallback? userCertificateSelectionCallback, System.Net.Security.EncryptionPolicy encryptionPolicy);
public SslStream (System.IO.Stream innerStream, bool leaveInnerStreamOpen, System.Net.Security.RemoteCertificateValidationCallback userCertificateValidationCallback, System.Net.Security.LocalCertificateSelectionCallback userCertificateSelectionCallback, System.Net.Security.EncryptionPolicy encryptionPolicy);
[System.MonoLimitation("encryptionPolicy is ignored")]
public SslStream (System.IO.Stream innerStream, bool leaveInnerStreamOpen, System.Net.Security.RemoteCertificateValidationCallback userCertificateValidationCallback, System.Net.Security.LocalCertificateSelectionCallback userCertificateSelectionCallback, System.Net.Security.EncryptionPolicy encryptionPolicy);
new System.Net.Security.SslStream : System.IO.Stream * bool * System.Net.Security.RemoteCertificateValidationCallback * System.Net.Security.LocalCertificateSelectionCallback * System.Net.Security.EncryptionPolicy -> System.Net.Security.SslStream
[<System.MonoLimitation("encryptionPolicy is ignored")>]
new System.Net.Security.SslStream : System.IO.Stream * bool * System.Net.Security.RemoteCertificateValidationCallback * System.Net.Security.LocalCertificateSelectionCallback * System.Net.Security.EncryptionPolicy -> System.Net.Security.SslStream
Public Sub New (innerStream As Stream, leaveInnerStreamOpen As Boolean, userCertificateValidationCallback As RemoteCertificateValidationCallback, userCertificateSelectionCallback As LocalCertificateSelectionCallback, encryptionPolicy As EncryptionPolicy)

パラメーター

innerStream
Stream

SslStream がデータの送受信に使用する Stream オブジェクト。

leaveInnerStreamOpen
Boolean

SslStream がデータの送受信に使用する Stream オブジェクトの閉じる動作を示すブール値。 このパラメーターは、内部ストリームを開いたままにするかどうかを示します。

userCertificateValidationCallback
RemoteCertificateValidationCallback

リモート側によって提供される証明書を検証する RemoteCertificateValidationCallback デリゲート。

userCertificateSelectionCallback
LocalCertificateSelectionCallback

認証に使用する証明書を選択する LocalCertificateSelectionCallback デリゲート。

encryptionPolicy
EncryptionPolicy

使用する EncryptionPolicy

属性
MonoLimitationAttribute

例外

innerStream が読み取り可能ではありません。

または innerStream が書き込み可能ではありません。

または encryptionPolicy が無効です。

innerStreamnullです。

または innerStreamNull が等価です。

注釈

パラメーターが に設定EncryptionPolicy.NoEncryptionされている場合は、Null 暗号のencryptionPolicy使用が必要です。

適用対象