SslStream コンストラクター

定義

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

オーバーロード

名前 説明
SslStream(Stream)

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

SslStream(Stream, Boolean)

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

SslStream(Stream, Boolean, RemoteCertificateValidationCallback)

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

SslStream(Stream, Boolean, RemoteCertificateValidationCallback, LocalCertificateSelectionCallback)

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

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

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

注釈

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

SslStream(Stream)

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

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

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

データの送受信にStreamによって使用されるSslStream オブジェクト。

例外

innerStream は読み取り不可能です。

-又は-

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

innerStreamnullです。

-又は-

innerStream == Null です。

注釈

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

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

適用対象

SslStream(Stream, Boolean)

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

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

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

データの送受信にStreamによって使用されるSslStream オブジェクト。

leaveInnerStreamOpen
Boolean

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

例外

innerStream は読み取り不可能です。

-又は-

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

innerStreamnullです。

-又は-

innerStream == Null です。

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

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

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

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

適用対象

SslStream(Stream, Boolean, RemoteCertificateValidationCallback)

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

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

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

データの送受信にStreamによって使用されるSslStream オブジェクト。

leaveInnerStreamOpen
Boolean

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

userCertificateValidationCallback
RemoteCertificateValidationCallback

リモート パーティによって提供される証明書の検証を担当する RemoteCertificateValidationCallback デリゲート。

例外

innerStream は読み取り不可能です。

-又は-

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

innerStreamnullです。

-又は-

innerStream == Null です。

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

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

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

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

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

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

Note

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

Note

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

適用対象

SslStream(Stream, Boolean, RemoteCertificateValidationCallback, LocalCertificateSelectionCallback)

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

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

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

データの送受信にStreamによって使用されるSslStream オブジェクト。

leaveInnerStreamOpen
Boolean

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

userCertificateValidationCallback
RemoteCertificateValidationCallback

リモート パーティによって提供される証明書の検証を担当する RemoteCertificateValidationCallback デリゲート。

userCertificateSelectionCallback
LocalCertificateSelectionCallback

認証に使用される証明書の選択を担当する LocalCertificateSelectionCallback デリゲート。

例外

innerStream は読み取り不可能です。

-又は-

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

innerStreamnullです。

-又は-

innerStream == Null です。

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

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

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

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

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

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

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

Note

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

適用対象

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

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

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

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

データの送受信にStreamによって使用されるSslStream オブジェクト。

leaveInnerStreamOpen
Boolean

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

userCertificateValidationCallback
RemoteCertificateValidationCallback

リモート パーティによって提供される証明書の検証を担当する RemoteCertificateValidationCallback デリゲート。

userCertificateSelectionCallback
LocalCertificateSelectionCallback

認証に使用される証明書の選択を担当する LocalCertificateSelectionCallback デリゲート。

encryptionPolicy
EncryptionPolicy

使用する EncryptionPolicy

例外

innerStream は読み取り不可能です。

-又は-

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

-又は-

encryptionPolicy が無効です。

innerStreamnullです。

-又は-

innerStream == Null です。

注釈

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

適用対象