SslStream 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 SslStream 類別的新執行個體。
多載
備註
為了防止他們 SslStream 關閉你供應的溪流,請使用 SslStream 建構器。
SslStream(Stream)
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
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)
參數
例外狀況
備註
如果設定檔中未指定加密政策的值,EncryptionPolicy則該實例預設為 。EncryptionPolicy.RequireEncryptionSslStream
當加密政策設定為 EncryptionPolicy.NoEncryption時,必須使用 Null 密碼。
適用於
SslStream(Stream, Boolean)
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
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)
參數
例外狀況
範例
以下程式碼範例示範呼叫此建構子。
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
備註
當你指定trueleaveStreamOpen參數時,關閉 不SslStream會影響innerStream串流;你必須在不再需要時明確關閉innerStream。
如果設定檔中未指定加密政策的值,EncryptionPolicy則該實例預設為 。EncryptionPolicy.RequireEncryptionSslStream
當加密政策設定為 EncryptionPolicy.NoEncryption時,必須使用 Null 密碼。
適用於
SslStream(Stream, Boolean, RemoteCertificateValidationCallback)
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
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)
參數
- userCertificateValidationCallback
- RemoteCertificateValidationCallback
RemoteCertificateValidationCallback一位負責驗證遠端方所提供憑證的代表。
例外狀況
範例
以下程式碼範例 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
備註
當你指定trueleaveStreamOpen參數時,關閉 不SslStream會影響innerStream串流;你必須在不再需要時明確關閉innerStream。
userCertificateValidationCallback 代理的 certificateErrors 參數包含通道安全支援提供者介面(SSPI)回傳的任何Windows錯誤碼。 代理所呼叫 userCertificateValidationCallback 方法的回傳值決定認證是否成功。
當 userCertificateValidationCallback 代理的方法被調用時,安全協定與密碼演算法已經被選擇。 你可以利用此方法判斷所選密碼演算法與強度是否足夠滿足你的應用需求。 如果沒有,方法應該還原 false 以防止 被 SslStream 建立。
如果設定檔中未指定加密政策的值,EncryptionPolicy則該實例預設為 。EncryptionPolicy.RequireEncryptionSslStream
當加密政策設定為 EncryptionPolicy.NoEncryption時,必須使用 Null 密碼。
Note
.NET 在 SSL 會話建立時會快取,並嘗試在可能的情況下重用快取會話以供後續請求。 當嘗試重用 SSL 會話時,Framework 會在認證時使用提供的第一個元素 X509Certificate2Collection (如果有的話),或在憑證集合為空時嘗試重用匿名會話。
Note
SSL 版本 2 協定不支援用戶端憑證。
適用於
SslStream(Stream, Boolean, RemoteCertificateValidationCallback, LocalCertificateSelectionCallback)
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
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)
參數
- userCertificateValidationCallback
- RemoteCertificateValidationCallback
RemoteCertificateValidationCallback一位負責驗證遠端方所提供憑證的代表。
- userCertificateSelectionCallback
- LocalCertificateSelectionCallback
一位 LocalCertificateSelectionCallback 負責選擇用於認證的憑證的代表。
例外狀況
範例
以下程式碼範例示範呼叫此建構子。 此範例是本類別更大 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))
備註
當你指定trueleaveStreamOpen參數時,關閉 不SslStream會影響innerStream串流;你必須在不再需要時明確關閉innerStream。
userCertificateValidationCallback 代理的 certificateErrors 參數包含通道安全支援提供者介面(SSPI)回傳的任何Windows錯誤碼。 代理所呼叫 userCertificateValidationCallback 方法的回傳值決定認證是否成功。
當 userCertificateValidationCallback 代理的方法被調用時,安全協定與密碼演算法已經被選擇。 你可以利用此方法判斷所選密碼演算法與強度是否足夠滿足你的應用需求。 如果沒有,方法應該還原 false 以防止 被 SslStream 建立。
當你的應用程式有多個憑證,必須動態選擇一個憑證時,代理 userCertificateSelectionCallback 很有用。 「MY」儲存中的憑證會傳遞給委託人所呼叫的方法。
如果設定檔中未指定加密政策的值,EncryptionPolicy則該實例預設為 。EncryptionPolicy.RequireEncryptionSslStream
當加密政策設定為 EncryptionPolicy.NoEncryption時,必須使用 Null 密碼。
Note
.NET 在 SSL 會話建立時會快取,並嘗試在可能的情況下重用快取會話以供後續請求。 當嘗試重用 SSL 會話時,Framework 會在認證時使用提供的第一個元素 X509Certificate2Collection (如果有的話),或在憑證集合為空時嘗試重用匿名會話。
適用於
SslStream(Stream, Boolean, RemoteCertificateValidationCallback, LocalCertificateSelectionCallback, EncryptionPolicy)
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
- 來源:
- SslStream.cs
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)
參數
- userCertificateValidationCallback
- RemoteCertificateValidationCallback
RemoteCertificateValidationCallback一位負責驗證遠端方所提供憑證的代表。
- userCertificateSelectionCallback
- LocalCertificateSelectionCallback
一位 LocalCertificateSelectionCallback 負責選擇用於認證的憑證的代表。
- encryptionPolicy
- EncryptionPolicy
例外狀況
備註
當參數 encryptionPolicy 設為 EncryptionPolicy.NoEncryption時,必須使用 Null 密碼。