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)

ソース:
SslStream.cs
ソース:
SslStream.cs
ソース:
SslStream.cs

指定した 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は に設定されます。

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

適用対象

SslStream(Stream, Boolean)

ソース:
SslStream.cs
ソース:
SslStream.cs
ソース:
SslStream.cs

指定した 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

注釈

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

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

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

適用対象

SslStream(Stream, Boolean, RemoteCertificateValidationCallback)

ソース:
SslStream.cs
ソース:
SslStream.cs
ソース:
SslStream.cs

指定した 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, 5000);
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,5000);
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, 5000)
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

注釈

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

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

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

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

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

Note

.NET は作成時に SSL セッションをキャッシュし、キャッシュされたセッションを後続の要求 (可能な場合) に再利用しようとします。 SSL セッションを再利用しようとすると、フレームワークは認証時に提供された の最初の X509Certificate2Collection 要素を使用するか (存在する場合)、証明書コレクションが空の場合は匿名セッションの再利用を試みます。

Note

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

適用対象

SslStream(Stream, Boolean, RemoteCertificateValidationCallback, LocalCertificateSelectionCallback)

ソース:
SslStream.cs
ソース:
SslStream.cs
ソース:
SslStream.cs

指定した 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,5000 );
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,5000);
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, 5000)
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))

注釈

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

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

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

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

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

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

Note

.NET は作成時に SSL セッションをキャッシュし、キャッシュされたセッションを後続の要求 (可能な場合) に再利用しようとします。 SSL セッションを再利用しようとすると、フレームワークは認証時に提供された の最初の X509Certificate2Collection 要素を使用するか (存在する場合)、証明書コレクションが空の場合は匿名セッションの再利用を試みます。

適用対象

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

ソース:
SslStream.IO.cs
ソース:
SslStream.cs
ソース:
SslStream.cs

指定した 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);
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

例外

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

または

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

または

encryptionPolicy が無効です。

innerStreamnullです。

または

innerStreamNull が等価です。

注釈

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

適用対象