当前位置: 首页>>代码示例>>C#>>正文


C# SslStream.EndAuthenticateAsClient方法代码示例

本文整理汇总了C#中System.Net.Security.SslStream.EndAuthenticateAsClient方法的典型用法代码示例。如果您正苦于以下问题:C# SslStream.EndAuthenticateAsClient方法的具体用法?C# SslStream.EndAuthenticateAsClient怎么用?C# SslStream.EndAuthenticateAsClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Security.SslStream的用法示例。


在下文中一共展示了SslStream.EndAuthenticateAsClient方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TlsConnect

 // guarantees to close the socket on error
 public static void TlsConnect(Socket sock, string host, RemoteCertificateValidationCallback rcvc, Action<Exception,SslStream> cb)
 {
     SslStream ssl = null;
     try {
         ssl = new SslStream (new NetworkStream (sock, true), false, rcvc);
         ssl.BeginAuthenticateAsClient (host, (ar) => {
             try {
                 ssl.EndAuthenticateAsClient (ar);
             } catch (Exception ex) {
                 ssl.Dispose ();
                 sock.Dispose ();
                 cb (ex, null);
                 return;
             }
             cb (null, ssl);
         }, null);
     } catch (Exception ex) {
         if (ssl != null)
             ssl.Dispose ();
         sock.Dispose ();
         cb (ex, null);
     }
 }
开发者ID:dnorman,项目名称:scamp,代码行数:24,代码来源:NetUtil.cs

示例2: Connect

        public void Connect(string user, string password, Region region, string clientVersion)
        {
            if (!isConnected)
            {
                Thread t = new Thread(() =>
                {
                    this.user = user;
                    this.password = password;
                    this.clientVersion = clientVersion;
                    //this.server = "127.0.0.1";
                    this.server = RegionInfo.GetServerValue(region);
                    this.loginQueue = RegionInfo.GetLoginQueueValue(region);
                    this.locale = RegionInfo.GetLocaleValue(region);
                    this.useGarena = RegionInfo.GetUseGarenaValue(region);

                    //Sets up our sslStream to riots servers
                    try
                    {
                        client = new TcpClient(server, 2099);
                    }
                    catch
                    {
                        Error("Riots servers are currently unavailable.", ErrorType.AuthKey);
                        Disconnect();
                        return;
                    }

                    //Check for riot webserver status
                    //along with gettin out Auth Key that we need for the login process.
                    if (useGarena)
                        if (!GetGarenaToken())
                            return;

                    if (!GetAuthKey())
                        return;

                    if (!GetIpAddress())
                        return;

                    sslStream = new SslStream(client.GetStream(), false, AcceptAllCertificates);
                    var ar = sslStream.BeginAuthenticateAsClient(server, null, null);
                    using (ar.AsyncWaitHandle)
                    {
                        if (ar.AsyncWaitHandle.WaitOne(-1))
                        {
                            sslStream.EndAuthenticateAsClient(ar);
                        }
                    }

                    if (!Handshake())
                        return;

                    BeginReceive();

                    if (!SendConnect())
                        return;

                    if (!Login())
                        return;

                    //StartHeartbeat();
                });
                t.IsBackground = true;
                t.Start();
            }
        }
开发者ID:ResQue1980,项目名称:LegendaryClient,代码行数:66,代码来源:PVPNetConnection.cs

示例3: ConnectSsl

 private Task<bool> ConnectSsl()
 {
     _logger.Verbose("Socket connected, starting SSL client authentication");
     //Stream mode: not the most performant but it has ssl support
     var targetHost = IPEndPoint.Address.ToString();
     //HostNameResolver is a sync operation but it can block
     //Use another thread
     return Task.Factory.StartNew(() =>
     {
         try
         {
             targetHost = SSLOptions.HostNameResolver(IPEndPoint.Address);
         }
         catch (Exception ex)
         {
             _logger.Error(String.Format("SSL connection: Can not resolve host name for address {0}. Using the IP address instead of the host name. This may cause RemoteCertificateNameMismatch error during Cassandra host authentication. Note that the Cassandra node SSL certificate's CN(Common Name) must match the Cassandra node hostname.", targetHost), ex);
         }
         return true;
     }).Then(_ =>
     {
         _logger.Verbose("Starting SSL authentication");
         var tcs = new TaskCompletionSource<bool>();
         var sslStream = new SslStream(new NetworkStream(_socket), false, SSLOptions.RemoteCertValidationCallback, null);
         _socketStream = sslStream;
         sslStream.BeginAuthenticateAsClient(targetHost, SSLOptions.CertificateCollection, SSLOptions.SslProtocol, SSLOptions.CheckCertificateRevocation, 
             sslAsyncResult =>
             {
                 try
                 {
                     sslStream.EndAuthenticateAsClient(sslAsyncResult);
                     tcs.TrySetResult(true);
                 }
                 catch (Exception ex)
                 {
                     tcs.TrySetException(ex);
                 }
             }, null);
         return tcs.Task;
     }).ContinueSync(_ =>
     {
         _logger.Verbose("SSL authentication successful");
         ReceiveAsync();
         return true;
     });
 }
开发者ID:GoldenCrystal,项目名称:csharp-driver,代码行数:45,代码来源:TcpSocket.cs

示例4: NegotiateStream

        private Stream NegotiateStream(Stream stream)
        {
            if (!_sslEnabled)
                return stream;

            var validateRemoteCertificate = new RemoteCertificateValidationCallback(
                (object sender,
                X509Certificate certificate,
                X509Chain chain,
                SslPolicyErrors sslPolicyErrors)
                =>
                {
                    if (sslPolicyErrors == SslPolicyErrors.None)
                        return true;

                    if (_configuration.SslPolicyErrorsBypassed)
                        return true;
                    else
                        _log(string.Format("Error occurred when validating remote certificate: [{0}], [{1}].",
                            this.RemoteEndPoint, sslPolicyErrors));

                    return false;
                });

            var sslStream = new SslStream(
                stream,
                false,
                validateRemoteCertificate,
                null);

            IAsyncResult ar = null;
            if (_configuration.SslClientCertificates == null || _configuration.SslClientCertificates.Count == 0)
            {
                ar = sslStream.BeginAuthenticateAsClient( // No client certificates are used in the authentication. The certificate revocation list is not checked during authentication.
                    _configuration.SslTargetHost, // The name of the server that will share this SslStream. The value specified for targetHost must match the name on the server's certificate.
                    null, _tcpClient);
            }
            else
            {
                ar = sslStream.BeginAuthenticateAsClient(
                    _configuration.SslTargetHost, // The name of the server that will share this SslStream. The value specified for targetHost must match the name on the server's certificate.
                    _configuration.SslClientCertificates, // The X509CertificateCollection that contains client certificates.
                    _configuration.SslEnabledProtocols, // The SslProtocols value that represents the protocol used for authentication.
                    _configuration.SslCheckCertificateRevocation, // A Boolean value that specifies whether the certificate revocation list is checked during authentication.
                    null, _tcpClient);
            }
            if (!ar.AsyncWaitHandle.WaitOne(ConnectTimeout))
            {
                Close(WebSocketCloseCode.TlsHandshakeFailed, "SSL/TLS handshake timeout.");
                throw new TimeoutException(string.Format(
                    "Negotiate SSL/TSL with remote [{0}] timeout [{1}].", this.RemoteEndPoint, ConnectTimeout));
            }
            sslStream.EndAuthenticateAsClient(ar);

            // When authentication succeeds, you must check the IsEncrypted and IsSigned properties 
            // to determine what security services are used by the SslStream. 
            // Check the IsMutuallyAuthenticated property to determine whether mutual authentication occurred.
            _log(string.Format(
                "Ssl Stream: SslProtocol[{0}], IsServer[{1}], IsAuthenticated[{2}], IsEncrypted[{3}], IsSigned[{4}], IsMutuallyAuthenticated[{5}], "
                + "HashAlgorithm[{6}], HashStrength[{7}], KeyExchangeAlgorithm[{8}], KeyExchangeStrength[{9}], CipherAlgorithm[{10}], CipherStrength[{11}].",
                sslStream.SslProtocol,
                sslStream.IsServer,
                sslStream.IsAuthenticated,
                sslStream.IsEncrypted,
                sslStream.IsSigned,
                sslStream.IsMutuallyAuthenticated,
                sslStream.HashAlgorithm,
                sslStream.HashStrength,
                sslStream.KeyExchangeAlgorithm,
                sslStream.KeyExchangeStrength,
                sslStream.CipherAlgorithm,
                sslStream.CipherStrength));

            return sslStream;
        }
开发者ID:cube3power,项目名称:Cowboy,代码行数:75,代码来源:WebSocketClient.cs


注:本文中的System.Net.Security.SslStream.EndAuthenticateAsClient方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。