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


C# SslStream.AuthenticateAsClientAsync方法代码示例

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


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

示例1: SslStream_StreamToStream_HandshakeAlert_Ok

        public async Task SslStream_StreamToStream_HandshakeAlert_Ok()
        {
            VirtualNetwork network = new VirtualNetwork();

            using (var clientStream = new VirtualNetworkStream(network, isServer: false))
            using (var serverStream = new VirtualNetworkStream(network, isServer: true))
            using (var client = new SslStream(clientStream, true, AllowAnyServerCertificate))
            using (var server = new SslStream(serverStream, true, FailClientCertificate))
            using (X509Certificate2 certificate = Configuration.Certificates.GetServerCertificate())
            {
                Task serverAuth = server.AuthenticateAsServerAsync(certificate);
                await client.AuthenticateAsClientAsync(certificate.GetNameInfo(X509NameType.SimpleName, false));

                byte[] buffer = new byte[1024];

                // Schannel semantics require that Decrypt is called to receive an alert.
                await client.WriteAsync(buffer, 0, buffer.Length);
                var exception = await Assert.ThrowsAsync<IOException>(() => client.ReadAsync(buffer, 0, buffer.Length));

                Assert.IsType<Win32Exception>(exception.InnerException);
                var win32ex = (Win32Exception)exception.InnerException;

                // The Schannel HResults for each alert are documented here: 
                // https://msdn.microsoft.com/en-us/library/windows/desktop/dd721886(v=vs.85).aspx
                Assert.Equal(SEC_E_CERT_UNKNOWN, (uint)win32ex.NativeErrorCode);

                await Assert.ThrowsAsync<AuthenticationException>(() => serverAuth);

                await Assert.ThrowsAsync<AuthenticationException>(() => server.WriteAsync(buffer, 0, buffer.Length));
                await Assert.ThrowsAsync<AuthenticationException>(() => server.ReadAsync(buffer, 0, buffer.Length));
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:32,代码来源:SslStreamAlertsTest.cs

示例2: SslStream_StreamToStream_ServerInitiatedCloseNotify_Ok

        public async Task SslStream_StreamToStream_ServerInitiatedCloseNotify_Ok()
        {
            VirtualNetwork network = new VirtualNetwork();

            using (var clientStream = new VirtualNetworkStream(network, isServer: false))
            using (var serverStream = new VirtualNetworkStream(network, isServer: true))
            using (var client = new SslStream(clientStream, true, AllowAnyServerCertificate))
            using (var server = new SslStream(serverStream))
            using (X509Certificate2 certificate = Configuration.Certificates.GetServerCertificate())
            {
                var handshake = new Task[2];

                handshake[0] = server.AuthenticateAsServerAsync(certificate);
                handshake[1] = client.AuthenticateAsClientAsync(certificate.GetNameInfo(X509NameType.SimpleName, false));

                await Task.WhenAll(handshake).TimeoutAfter(TestConfiguration.PassingTestTimeoutMilliseconds);

                var readBuffer = new byte[1024];

                await server.ShutdownAsync();
                int bytesRead = await client.ReadAsync(readBuffer, 0, readBuffer.Length);
                // close_notify received by the client.
                Assert.Equal(0, bytesRead);

                await client.ShutdownAsync();
                bytesRead = await server.ReadAsync(readBuffer, 0, readBuffer.Length);
                // close_notify received by the server.
                Assert.Equal(0, bytesRead);
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:30,代码来源:SslStreamAlertsTest.cs

示例3: SslStream_AuthenticateAsClient_AllSupported_Success

 public async Task SslStream_AuthenticateAsClient_AllSupported_Success()
 {
     SslStream stream = new SslStream(new FakeStream());
     await stream.AuthenticateAsClientAsync(
         "host",
         null,
         SslProtocolSupport.SupportedSslProtocols,
         false);
 }
开发者ID:Rayislandstyle,项目名称:corefx,代码行数:9,代码来源:SslStreamAllowedProtocolsTest.cs

示例4: SslStream_SendReceiveOverNetworkStream_Ok

        public async void SslStream_SendReceiveOverNetworkStream_Ok()
        {
            TcpListener listener = new TcpListener(IPAddress.Any, 0);

            using (X509Certificate2 serverCertificate = Configuration.Certificates.GetServerCertificate())
            using (TcpClient client = new TcpClient())
            {
                listener.Start();

                Task clientConnectTask = client.ConnectAsync(IPAddress.Loopback, ((IPEndPoint)listener.LocalEndpoint).Port);
                Task<TcpClient> listenerAcceptTask = listener.AcceptTcpClientAsync();

                await Task.WhenAll(clientConnectTask, listenerAcceptTask);

                TcpClient server = listenerAcceptTask.Result;
                using (SslStream clientStream = new SslStream(
                    client.GetStream(),
                    false,
                    new RemoteCertificateValidationCallback(ValidateServerCertificate),
                    null,
                    EncryptionPolicy.RequireEncryption))
                using (SslStream serverStream = new SslStream(
                    server.GetStream(),
                    false,
                    null,
                    null,
                    EncryptionPolicy.RequireEncryption))
                {

                    Task clientAuthenticationTask = clientStream.AuthenticateAsClientAsync(
                        serverCertificate.GetNameInfo(X509NameType.SimpleName, false),
                        null,
                        SslProtocols.Tls12,
                        false);

                    Task serverAuthenticationTask = serverStream.AuthenticateAsServerAsync(
                        serverCertificate,
                        false,
                        SslProtocols.Tls12,
                        false);

                    await Task.WhenAll(clientAuthenticationTask, serverAuthenticationTask);

                    byte[] readBuffer = new byte[256];
                    Task<int> readTask = clientStream.ReadAsync(readBuffer, 0, readBuffer.Length);

                    byte[] writeBuffer = new byte[256];
                    Task writeTask = clientStream.WriteAsync(writeBuffer, 0, writeBuffer.Length);

                    bool result = Task.WaitAll(
                        new Task[1] { writeTask }, 
                        TestConfiguration.PassingTestTimeoutMilliseconds);

                    Assert.True(result, "WriteAsync timed-out.");
                }
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:57,代码来源:SslStreamNetworkStreamTest.cs

示例5: CertificateValidationClientServer_EndToEnd_Ok

        public async Task CertificateValidationClientServer_EndToEnd_Ok()
        {
            IPEndPoint endPoint = new IPEndPoint(IPAddress.IPv6Loopback, 0);
            var server = new TcpListener(endPoint);
            server.Start();

            using (var clientConnection = new TcpClient(AddressFamily.InterNetworkV6))
            {
                IPEndPoint serverEndPoint = (IPEndPoint)server.LocalEndpoint;

                Task clientConnect = clientConnection.ConnectAsync(serverEndPoint.Address, serverEndPoint.Port);
                Task<TcpClient> serverAccept = server.AcceptTcpClientAsync();

                Assert.True(
                    Task.WaitAll(
                        new Task[] { clientConnect, serverAccept }, 
                        TestConfiguration.TestTimeoutSeconds * 1000),
                    "Client/Server TCP Connect timed out.");

                using (TcpClient serverConnection = await serverAccept)
                using (SslStream sslClientStream = new SslStream(
                    clientConnection.GetStream(),
                    false,
                    ClientSideRemoteServerCertificateValidation))
                using (SslStream sslServerStream = new SslStream(
                    serverConnection.GetStream(),
                    false,
                    ServerSideRemoteClientCertificateValidation))

                {
                    string serverName = _serverCertificate.GetNameInfo(X509NameType.SimpleName, false);
                    string clientName = _clientCertificate.GetNameInfo(X509NameType.SimpleName, false);

                    var clientCerts = new X509CertificateCollection();
                    clientCerts.Add(_clientCertificate);

                    Task clientAuthentication = sslClientStream.AuthenticateAsClientAsync(
                        serverName,
                        clientCerts,
                        TestConfiguration.DefaultSslProtocols,
                        false);

                    Task serverAuthentication = sslServerStream.AuthenticateAsServerAsync(
                        _serverCertificate,
                        true,
                        TestConfiguration.DefaultSslProtocols,
                        false);

                    Assert.True(
                        Task.WaitAll(
                            new Task[] { clientAuthentication, serverAuthentication }, 
                            TestConfiguration.TestTimeoutSeconds * 1000),
                        "Client/Server Authentication timed out.");
                }
            }
        }
开发者ID:mmajcica,项目名称:corefx,代码行数:56,代码来源:CertificateValidationClientServer.cs

示例6: SslStream_AuthenticateAsClient_AllUnsuported_Fails

 public async Task SslStream_AuthenticateAsClient_AllUnsuported_Fails()
 {
     SslStream stream = new SslStream(new FakeStream());
     await Assert.ThrowsAsync<NotSupportedException>(
         () => stream.AuthenticateAsClientAsync(
             "host",
             null,
             SslProtocolSupport.UnsupportedSslProtocols,
             false));
 }
开发者ID:Rayislandstyle,项目名称:corefx,代码行数:10,代码来源:SslStreamAllowedProtocolsTest.cs

示例7: CertificateValidationRemoteServer_EndToEnd_Ok

        public async Task CertificateValidationRemoteServer_EndToEnd_Ok()
        {
            using (var client = new TcpClient(AddressFamily.InterNetwork))
            {
                await client.ConnectAsync(TestSettings.Http.SecureHost, 443);

                using (SslStream sslStream = new SslStream(client.GetStream(), false, RemoteHttpsCertValidation, null))
                {
                    await sslStream.AuthenticateAsClientAsync(TestSettings.Http.SecureHost);
                }
            }
        }
开发者ID:charygao,项目名称:corefx,代码行数:12,代码来源:CertificateValidationRemoteServer.cs

示例8: CertificateValidationRemoteServer_EndToEnd_Ok

        public async Task CertificateValidationRemoteServer_EndToEnd_Ok()
        {
            using (var client = new TcpClient(AddressFamily.InterNetwork))
            {
                await client.ConnectAsync(Configuration.Security.TlsServer.IdnHost, Configuration.Security.TlsServer.Port);

                using (SslStream sslStream = new SslStream(client.GetStream(), false, RemoteHttpsCertValidation, null))
                {
                    await sslStream.AuthenticateAsClientAsync(Configuration.Security.TlsServer.IdnHost);
                }
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:12,代码来源:CertificateValidationRemoteServer.cs

示例9: ServerRequireEncryption_ClientNoEncryption_NoConnect

 public async Task ServerRequireEncryption_ClientNoEncryption_NoConnect()
 {
     using (var serverRequireEncryption = new DummyTcpServer(
         new IPEndPoint(IPAddress.Loopback, 0), EncryptionPolicy.RequireEncryption))
     using (var client = new TcpClient())
     {
         await client.ConnectAsync(serverRequireEncryption.RemoteEndPoint.Address, serverRequireEncryption.RemoteEndPoint.Port);
         using (var sslStream = new SslStream(client.GetStream(), false, AllowAnyServerCertificate, null, EncryptionPolicy.NoEncryption))
         {
             await Assert.ThrowsAsync<IOException>(() =>
                 sslStream.AuthenticateAsClientAsync("localhost", null, SslProtocolSupport.DefaultSslProtocols, false));
         }
     }
 }
开发者ID:ESgarbi,项目名称:corefx,代码行数:14,代码来源:ServerRequireEncryptionTest.cs

示例10: TransportContext_ConnectToServerWithSsl_GetExpectedChannelBindings

        public async Task TransportContext_ConnectToServerWithSsl_GetExpectedChannelBindings()
        {
            using (var testServer = new DummyTcpServer(
                new IPEndPoint(IPAddress.Loopback, 0), EncryptionPolicy.RequireEncryption))
            using (var client = new TcpClient())
            {
                await client.ConnectAsync(testServer.RemoteEndPoint.Address, testServer.RemoteEndPoint.Port);

                using (var sslStream = new SslStream(client.GetStream(), false, AllowAnyServerCertificate, null, EncryptionPolicy.RequireEncryption))
                {
                    await sslStream.AuthenticateAsClientAsync("localhost", null, SslProtocols.Tls, false);

                    TransportContext context = sslStream.TransportContext;
                    CheckTransportContext(context);
                }
            }
        }
开发者ID:Rayislandstyle,项目名称:corefx,代码行数:17,代码来源:TransportContextTest.cs

示例11: ClientDefaultEncryption_ServerAllowNoEncryption_ConnectWithEncryption

        public async Task ClientDefaultEncryption_ServerAllowNoEncryption_ConnectWithEncryption()
        {
            using (var serverAllowNoEncryption = new DummyTcpServer(
                new IPEndPoint(IPAddress.Loopback, 0), EncryptionPolicy.AllowNoEncryption))
            using (var client = new TcpClient())
            {
                await client.ConnectAsync(serverAllowNoEncryption.RemoteEndPoint.Address, serverAllowNoEncryption.RemoteEndPoint.Port);

                using (var sslStream = new SslStream(client.GetStream(), false, AllowAnyServerCertificate, null))
                {
                    await sslStream.AuthenticateAsClientAsync("localhost", null, SslProtocolSupport.DefaultSslProtocols, false);
                    _log.WriteLine("Client authenticated to server({0}) with encryption cipher: {1} {2}-bit strength",
                        serverAllowNoEncryption.RemoteEndPoint, sslStream.CipherAlgorithm, sslStream.CipherStrength);
                    Assert.True(sslStream.CipherAlgorithm != CipherAlgorithmType.Null, "Cipher algorithm should not be NULL");
                    Assert.True(sslStream.CipherStrength > 0, "Cipher strength should be greater than 0");
                }
            }
        }
开发者ID:Rayislandstyle,项目名称:corefx,代码行数:18,代码来源:ClientDefaultEncryptionTest.cs

示例12: SslStream_StreamToStream_Authentication_Success

        public void SslStream_StreamToStream_Authentication_Success()
        {
            VirtualNetwork network = new VirtualNetwork();

            using (var clientStream = new VirtualNetworkStream(network, isServer: false))
            using (var serverStream = new VirtualNetworkStream(network, isServer: true))
            using (var client = new SslStream(clientStream, false, AllowAnyServerCertificate))
            using (var server = new SslStream(serverStream))
            {
                X509Certificate2 certificate = TestConfiguration.GetServerCertificate();
                Task[] auth = new Task[2];
                auth[0] = client.AuthenticateAsClientAsync(certificate.GetNameInfo(X509NameType.SimpleName, false));
                auth[1] = server.AuthenticateAsServerAsync(certificate);

                bool finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);
                Assert.True(finished, "Handshake completed in the allotted time");
            }
        }
开发者ID:benpye,项目名称:corefx,代码行数:18,代码来源:SslStreamStreamToStreamTest.cs

示例13: SslStream_StreamToStream_Authentication_Success

        public void SslStream_StreamToStream_Authentication_Success()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
            using (var serverStream = new FakeNetworkStream(true, network))
            using (var client = new SslStream(clientStream, false, AllowAnyServerCertificate))
            using (var server = new SslStream(serverStream))
            {
                X509Certificate2 certificate = TestConfiguration.GetServerCertificate();
                Task[] auth = new Task[2];
                auth[0] = client.AuthenticateAsClientAsync(certificate.Subject);
                auth[1] = server.AuthenticateAsServerAsync(certificate);

                bool finished = Task.WaitAll(auth, TimeSpan.FromSeconds(3));
                Assert.True(finished, "Handshake completed in the allotted time");
            }
        }
开发者ID:antonfirsov,项目名称:corefx,代码行数:18,代码来源:SslStreamStreamToStreamTest.cs

示例14: RunTest

            public async Task RunTest()
            {
                using (var tcp = new TcpClient())
                {
                    _log.WriteLine("[Client] Connecting to {0}:{1}", _server, _port);
                    await tcp.ConnectAsync(_server, _port);
                    using (var tls = new SslStream(tcp.GetStream(), false, CertificateValidation))
                    {
                        _log.WriteLine("[Client] Connected. Authenticating...");
                        await tls.AuthenticateAsClientAsync(_server, null, System.Security.Authentication.SslProtocols.Tls, false);

                        string requestString = "GET / HTTP/1.0\r\nHost: servername.test.contoso.com\r\nUser-Agent: Testing application\r\n\r\n";
                        byte[] requestBuffer = Encoding.UTF8.GetBytes(requestString);

                        _log.WriteLine("[Client] Sending request ({0} Bytes)", requestBuffer.Length);

                        await tls.WriteAsync(requestBuffer, 0, requestBuffer.Length);

                        _log.WriteLine("[Client] Waiting for reply...");

                        int bytesRead = 0;
                        int chunks = 0;
                        do
                        {
                            byte[] responseBuffer = new byte[2048];
                            bytesRead = await tls.ReadAsync(responseBuffer, 0, responseBuffer.Length);

                            if (bytesRead > 0)
                            {
                                string responseString = Encoding.UTF8.GetString(responseBuffer, 0, bytesRead);
                                _log.WriteLine("[Client {0}: {2} Bytes] Response: <<<<<{1}>>>>>", chunks, responseString, bytesRead);
                            }

                            if (bytesRead == 1 && chunks == 0)
                            {
                                AuxRecordDetected = true;
                            }

                            chunks++;
                        }
                        while (bytesRead > 0);
                    }
                }
            }
开发者ID:alessandromontividiu03,项目名称:corefx,代码行数:44,代码来源:SslStreamSchSendAuxRecordTest.cs

示例15: ServerAllowNoEncryption_ClientRequireEncryption_ConnectWithEncryption

        public async Task ServerAllowNoEncryption_ClientRequireEncryption_ConnectWithEncryption()
        {
            using (var serverAllowNoEncryption = new DummyTcpServer(
                new IPEndPoint(IPAddress.Loopback, 0), EncryptionPolicy.AllowNoEncryption))
            using (var client = new TcpClient())
            {
                await client.ConnectAsync(serverAllowNoEncryption.RemoteEndPoint.Address, serverAllowNoEncryption.RemoteEndPoint.Port);

                using (var sslStream = new SslStream(client.GetStream(), false, AllowAnyServerCertificate, null, EncryptionPolicy.RequireEncryption))
                {
                    await sslStream.AuthenticateAsClientAsync("localhost", null, SslProtocolSupport.DefaultSslProtocols, false);
                    _log.WriteLine("Client({0}) authenticated to server({1}) with encryption cipher: {2} {3}-bit strength",
                        client.Client.LocalEndPoint, client.Client.RemoteEndPoint,
                        sslStream.CipherAlgorithm, sslStream.CipherStrength);
                    Assert.NotEqual(CipherAlgorithmType.Null, sslStream.CipherAlgorithm);
                    Assert.True(sslStream.CipherStrength > 0);
                }
            }
        }
开发者ID:alux-xu,项目名称:corefx,代码行数:19,代码来源:ServerAllowNoEncryptionTest.cs


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