本文整理汇总了C#中System.Net.Security.SslStream.AuthenticateAsClient方法的典型用法代码示例。如果您正苦于以下问题:C# SslStream.AuthenticateAsClient方法的具体用法?C# SslStream.AuthenticateAsClient怎么用?C# SslStream.AuthenticateAsClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Security.SslStream
的用法示例。
在下文中一共展示了SslStream.AuthenticateAsClient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test
static void Test()
{
// Connect as client to port 1300
string server = "127.0.0.1";
TcpClient client = new TcpClient(server, 1300);
// Create a secure stream
using (SslStream sslStream = new SslStream(client.GetStream(), false,
new RemoteCertificateValidationCallback(ValidateServerCertificate), null))
{
sslStream.AuthenticateAsClient(server);
// ... Send and read data over the stream
byte[] buffer = new byte[1024];
int n = sslStream.Read(buffer, 0, 1024);
string _message = System.Text.Encoding.UTF8.GetString(buffer, 0, n);
System.Console.WriteLine("Client said: " + _message);
}
// Disconnect and close the client
client.Close();
}
示例2: Client
public Client(String host, Int32 port)
{
try
{
clientName = Dns.GetHostName();
}
catch (SocketException se)
{
MessageBox.Show("ERROR: Could not retrieve client's DNS hostname. Please try again." + se.Message + ".", "Client Socket Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
serverName = host;
gamePort = port;
client = new TcpClient(host, port);
netStream = client.GetStream();
reader = new StreamReader(netStream);
writer = new StreamWriter(netStream);
ssl = new SslStream(netStream, false, new RemoteCertificateValidationCallback(ValidateCert));
cert = new X509Certificate2("server.crt");
ssl.AuthenticateAsClient(serverName);
writer.AutoFlush = true;
}
示例3: EnableSsl
/// <summary>
/// Use SSL to encrypt the communication
/// </summary>
public void EnableSsl()
{
SslStream sslStream = new SslStream(clientStream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
if (cCollection.Count > 0)
{
// A client side certificate was added, authenticate with certificate
sslStream.AuthenticateAsClient(serverHostname, cCollection, System.Security.Authentication.SslProtocols.Default, true);
}
else
sslStream.AuthenticateAsClient(serverHostname);
streamToUse = sslStream;
}
示例4: connectForwardServerCallBack
public static void connectForwardServerCallBack(IAsyncResult ar)
{
ProxyConnection conn = (ProxyConnection)ar.AsyncState;
conn.serverSocket.EndConnect(ar);
ProxySwapDataTask proxy = new ProxySwapDataTask(conn);
//now we have both server socket and client socket, we can pass the data back and forth for both side
System.Diagnostics.Debug.Assert(true == conn.clientSocket.Connected);
System.Diagnostics.Debug.Assert(true == conn.serverSocket.Connected);
WinTunnel.WriteTextToConsole(string.Format("ProxyConnection#{0}-- client socket or server socket start receiving data....",
conn.connNumber));
WinTunnel.connMgr.AddConnection(conn);
conn.clientStream = conn.clientSocket.GetStream();
if (conn.m_bHttpsClient)
{
SslStream sslStream = new SslStream(conn.clientSocket.GetStream(), false);
sslStream.AuthenticateAsServer(WinTunnel.CertificateForClientConnection, false, SslProtocols.Tls, true);
conn.clientStream = sslStream;
}
conn.serverStream = conn.serverSocket.GetStream();
if (conn.m_bHttpsServer)
{
SslStream sslStream = new SslStream(conn.serverStream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
sslStream.AuthenticateAsClient(conn.serverName);
conn.serverStream = sslStream;
}
conn.clientStream.BeginRead(conn.clientReadBuffer, 0, ProxyConnection.BUFFER_SIZE, new AsyncCallback(clientReadCallBack), conn);
//Read data from the server socket
conn.serverStream.BeginRead(conn.serverReadBuffer, 0, ProxyConnection.BUFFER_SIZE, new AsyncCallback(serverReadCallBack), conn);
}
示例5: Validar
public static bool Validar(string host, int porta)
{
try
{
using (var client = new TcpClient())
{
client.Connect(host, porta);
using (var stream = client.GetStream())
using (var sslStream = new SslStream(stream))
{
sslStream.AuthenticateAsClient(host);
using (var writer = new StreamWriter(sslStream))
using (var reader = new StreamReader(sslStream))
{
var retorno = reader.ReadLine();
return retorno != null && (retorno.Contains("250") || retorno.Contains("220"));
}
}
}
}
catch (Exception)
{
return false;
}
}
示例6: handler
public void handler()
{
DataPacket data = null;
sslStream = new SslStream(tcpClient.GetStream(), false, new RemoteCertificateValidationCallback(validateServerCertificate), null);
try
{
sslStream.AuthenticateAsClient(ipAdress);
}
catch
{
Console.WriteLine("Could not connect to server.");
}
for(; ; )
{
if (sslStream != null && sslStream.CanRead)
{
try
{
data = formatter.Deserialize(sslStream) as DataPacket;
handleDataPacket(data);
}
catch
{
Console.WriteLine("Received data from server.");
//disconnect();
}
}
Thread.Sleep(100);
}
}
示例7: Connect
public override Stream Connect(Uri uri, bool requireAuthentication) {
var rawStream = base.Connect(uri, requireAuthentication);
try {
var sslStream = new SslStream(rawStream, false, (sender, cert, chain, errs) => {
if (errs == SslPolicyErrors.None || !requireAuthentication) {
return true;
}
string errText = string.Format("Could not establish secure connection to {0} because of the following SSL issues:\n\n", uri);
if ((errs & SslPolicyErrors.RemoteCertificateNotAvailable) != 0) {
errText += "- no remote certificate provided\n";
}
if ((errs & SslPolicyErrors.RemoteCertificateNameMismatch) != 0) {
errText += "- remote certificate name does not match hostname\n";
}
if ((errs & SslPolicyErrors.RemoteCertificateChainErrors) != 0) {
errText += "- remote certificate is not trusted\n";
}
throw new AuthenticationException(errText);
});
sslStream.AuthenticateAsClient(uri.Host);
rawStream = null;
return sslStream;
} catch (IOException ex) {
throw new ConnectionException(ConnErrorMessages.RemoteNetworkError, ex);
} finally {
if (rawStream != null) {
rawStream.Dispose();
}
}
}
示例8: Build
/// <summary>
/// Build a new SSL steam.
/// </summary>
/// <param name="channel">Channel which will use the stream</param>
/// <param name="socket">Socket to wrap</param>
/// <returns>Stream which is ready to be used (must have been validated)</returns>
public SslStream Build(ITcpChannel channel, Socket socket)
{
var ns = new NetworkStream(socket);
var stream = new SslStream(ns, true, OnRemoteCertifiateValidation);
try
{
X509CertificateCollection certificates = null;
if (Certificate != null)
{
certificates = new X509CertificateCollection(new[] { Certificate });
}
stream.AuthenticateAsClient(CommonName, certificates, Protocols, false);
}
catch (IOException err)
{
throw new InvalidOperationException("Failed to authenticate " + socket.RemoteEndPoint, err);
}
catch (ObjectDisposedException err)
{
throw new InvalidOperationException("Failed to create stream, did client disconnect directly?", err);
}
catch (AuthenticationException err)
{
throw new InvalidOperationException("Failed to authenticate " + socket.RemoteEndPoint, err);
}
return stream;
}
示例9: SslClient
public SslClient(string ip, int port, UserCertificate certificate = null, int bufferSize = 1024) : base(ip, port)
{
try
{
_bufferSize = bufferSize;
if (certificate != null)
{
_certificate = certificate;
}
else
{
_certificate = new X509Certificate2();
}
_stream = new SslStream(GetStream(), false, ServerCertificateValidation, UserCertificateSelection);
_stream.AuthenticateAsClient(ip);
}
catch (AuthenticationException err)
{
ErrorOnAuthentication();
}
catch (SocketException)
{
ErrorOnAuthentication();
}
catch (Win32Exception)
{
ErrorOnAuthentication();
}
}
示例10: Discover
public ServiceEndPoint Discover(Uri remoteUri)
{
try
{
using (var client = CreateTcpClient())
{
client.ConnectWithTimeout(remoteUri, HalibutLimits.TcpClientConnectTimeout);
using (var stream = client.GetStream())
{
using (var ssl = new SslStream(stream, false, ValidateCertificate))
{
ssl.AuthenticateAsClient(remoteUri.Host, new X509Certificate2Collection(), SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, false);
ssl.Write(HelloLine, 0, HelloLine.Length);
ssl.Flush();
if (ssl.RemoteCertificate == null)
throw new Exception("The server did not provide an SSL certificate");
return new ServiceEndPoint(remoteUri, new X509Certificate2(ssl.RemoteCertificate).Thumbprint);
}
}
}
}
catch (Exception ex)
{
throw new HalibutClientException(ex.Message, ex);
}
}
示例11: InternalSslSocketHttp
static byte[] InternalSslSocketHttp(IPEndPoint endpoint, HttpArgs args, HttpMethod method, X509CertificateCollection certificates)
{
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
try
{
client.Connect(endpoint);
if (client.Connected)
{
using (SslStream stream = new SslStream(new NetworkStream(client), false, ValidateServerCertificate, null))
{
stream.AuthenticateAsClient("ServerName", certificates, SslProtocols.Tls, false);
if (stream.IsAuthenticated)
{
//生成协议包
byte[] buff = HttpClient.ParseHttpArgs(method, args);
stream.Write(buff, 0, buff.Length);
stream.Flush();
return ParseSslResponse(endpoint, stream, args, certificates);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
return null;
}
示例12: runClient
private void runClient()
{
TcpClient client = new TcpClient(server, port);
Console.WriteLine("Client connected ...");
// Create ssl stream
SslStream stream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(validateServerCertificate), null);
stream.AuthenticateAsClient(server);
// write message to server
byte[] output = Encoding.UTF8.GetBytes("Message from client :D<EOF>");
stream.Write(output);
stream.Flush();
// read message from server
string input = readMessage(stream);
Console.WriteLine("Received: {0}", input);
// close everything
stream.Close();
client.Close();
Console.WriteLine("Client closed connection ...");
// Press any key to continue ...
Console.ReadKey();
}
示例13: RunClient
public static string RunClient(string serverName,string activation_info,ref string buffer)
{
TcpClient client = new TcpClient(serverName,443);
SslStream sslStream = new SslStream(
client.GetStream(),
false,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null
);
try
{
sslStream.AuthenticateAsClient(serverName);
}
catch (AuthenticationException e)
{
if (e.InnerException != null)
{
}
client.Close();
Environment.Exit(-1);
}
byte[] messsage = Encoding.UTF8.GetBytes(activation_info + "\n<EOF>");
sslStream.Write(messsage);
sslStream.Flush();
string serverMessage = ReadMessage(sslStream);
client.Close();
buffer = serverMessage;
return serverMessage;
}
示例14: Connect
public override string Connect()
{
string response;
try
{
_Client = new TcpClient();
_Client.NoDelay = false;
_Client.ReceiveTimeout = 60000;
_Client.SendTimeout = 60000;
_Client.Connect(_Address, _Port);
NetworkStream netStream = _Client.GetStream();
_SslStream = new SslStream(netStream, false, CertificationValidationCallback);
_SslStream.ReadTimeout = 60000;
_SslStream.WriteTimeout = 600000;
_SslStream.AuthenticateAsClient(_CertificateAddress);
response = GetMessage(_SslStream);
}
catch (Exception ex)
{
string err = "";
err += "Problem occured while connecting. Address or Port may be invalid.";
err += ex.Message;
if (ex.InnerException != null)
err += ex.InnerException.Message;
throw new ProtocolConnectionException(err);
}
return response;
}
示例15: CreateClient
private static TcpConnection CreateClient(string Hostname, int port, bool IsSecure)
{
var client = new TcpClient(Hostname, port);
var stream = (Stream)client.GetStream();
if (IsSecure)
{
var sslStream = (SslStream)null;
try
{
sslStream = new SslStream(stream);
sslStream.AuthenticateAsClient(Hostname);
stream = (Stream)sslStream;
}
catch
{
if (sslStream != null)
sslStream.Dispose();
throw;
}
}
return new TcpConnection()
{
HostName = Hostname,
port = port,
IsSecure = IsSecure,
TcpClient = client,
ServerStreamReader = new CustomBinaryReader(stream, Encoding.ASCII),
Stream = stream
};
}