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


C# SslStream.AuthenticateAsServer方法代码示例

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


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

示例1: WebSocketClient

	//Encryption
	internal WebSocketClient(Socket s,X509Certificate cert)
	{	
		this.Socket = s;
		var ns = new NetworkStream(s,false);
		var ss = new SslStream(ns, false);
		ss.AuthenticateAsServer(cert,false,SslProtocols.Tls12,false);
		this.stream = ss;
	}
开发者ID:Carrot031,项目名称:websocket-cs,代码行数:9,代码来源:WebSocketClient.cs

示例2: BasicServerTest

			public void BasicServerTest() {
				try {
					testName = "BasicServerTest";
					AcceptConnection(); // sets the client member
					sslStream = new SslStream(client.GetStream(), false);
					sslStream.AuthenticateAsServer(testServer.serverCertificate);
					// Do the server read, and write of the messages
					if (DoServerReadWrite()) {
						Shutdown(true);
					}
					else {
						Shutdown(false);
					}
				}
				catch (Exception) {
					Shutdown(false);
				}
			}
开发者ID:challal,项目名称:scallion,代码行数:18,代码来源:TestServer.cs

示例3: AdvancedServerTest

			public void AdvancedServerTest() {
				serverRemoteCertificateValidationCallback = new RemoteCertificateValidationHandler(ValidateRemoteCert);

				try {
					testName = "AdvancedServerTest";
					AcceptConnection(); // sets the client member
					sslStream = new SslStream(client.GetStream(), false, serverRemoteCertificateValidationCallback);
					sslStream.AuthenticateAsServer(testServer.serverCertificate, true, testServer.serverCAChain, SslProtocols.Tls, SslStrength.All, true);

					// Verify mutual authentication
					if (!sslStream.IsMutuallyAuthenticated) {
						Console.WriteLine("{0} failed - stream is not mutually authenticated", testName);
						Shutdown(false);
						return;
					}

					// Verify protocol
					if (sslStream.SslProtocol != SslProtocols.Tls) {
						Console.WriteLine("{0} failed - negotiated non Tls connection", testName);
						Shutdown(false);
						return;
					}
					// Verify cipher strength
					if (sslStream.CipherStrength < 256) {
						Console.WriteLine("{0} failed - negotiated less than 256bit cipher", testName);
						Shutdown(false);
						return;
					}
					// Do the server read, and write of the messages
					if (DoServerReadWrite()) {
						Shutdown(true);
					}
					else {
						Shutdown(false);
					}
				}
				catch (Exception) {
					Shutdown(false);
				}
			}
开发者ID:challal,项目名称:scallion,代码行数:40,代码来源:TestServer.cs

示例4: IntermediateServerTest

			public void IntermediateServerTest() {
				try {
					testName = "IntermediateServerTest";
					AcceptConnection(); // sets the client member
					sslStream = new SslStream(client.GetStream(), false);
					sslStream.AuthenticateAsServer(testServer.serverCertificate, false, null, SslProtocols.Tls, SslStrength.All, false);

					// Verify protocol
					if (sslStream.SslProtocol != SslProtocols.Tls) {
						Console.WriteLine("{0} failed - negotiated non Tls connection", testName);
						Shutdown(false);
						return;
					}
					// Verify cipher strength
					if (sslStream.CipherStrength < 256) {
						Console.WriteLine("{0} failed - negotiated less than 256bit cipher", testName);
						Shutdown(false);
						return;
					}
					//Verify cipher
					if (sslStream.CipherAlgorithm != CipherAlgorithmType.Aes256) {
						Console.WriteLine("{0} failed - negotiated cipher was not AES256", testName);
						Shutdown(false);
						return;
					}

					// Do the server read, and write of the messages
					if (DoServerReadWrite()) {
						Shutdown(true);
					}
					else {
						Shutdown(false);
					}
				}
				catch (Exception) {
					Shutdown(false);
				}
			}
开发者ID:challal,项目名称:scallion,代码行数:38,代码来源:TestServer.cs

示例5: Run

        public void Run()
        {
            Trace.TraceInformation("Server started on port: " + _server.Port);
            while (!IsStopped())
            {
                try
                {
                    TcpClient connection = null;
                    Stream stream = null;
                    try
                    {
                        connection = _socket.AcceptTcpClient();
                        stream = connection.GetStream();
                        if (_server.UseSSL)
                        {
                            SslStream sslStream = new SslStream(stream, false);
                            stream = sslStream;
                            sslStream.AuthenticateAsServer(_server.ServerCertificate,
                                                           false,
                                                           SslProtocols.Tls,
                                                           false);
                        }

                        ConnectionProcessor processor =
                            new ConnectionProcessor(_server,
                                                    new RemoteFrameworkConnection(connection, stream));
                        Thread thread = new Thread(processor.Run);
                        thread.IsBackground = false;
                        thread.Start();
                    }
                    catch (Exception)
                    {
                        if (stream != null)
                        {
                            try { stream.Close(); }
                            catch (Exception) { }
                        }
                        if (connection != null)
                        {
                            try { connection.Close(); }
                            catch (Exception) { }
                        }
                        throw;
                    }
                }
                catch (Exception e)
                {
                    //log the error unless it's because we've stopped
                    if (!IsStopped() || !(e is SocketException))
                    {
                        TraceUtil.TraceException("Error processing request", e);
                    }
                    //wait a second before trying again
                    if (!IsStopped())
                    {
                        Thread.Sleep(1000);
                    }
                }
            }
        }
开发者ID:Tirasa,项目名称:ConnId,代码行数:60,代码来源:Server.cs

示例6: DoHandshake

 protected override bool DoHandshake(SslStream clientSslStream, SslStream serverSslStream)
 {
     using (X509Certificate2 certificate = Configuration.Certificates.GetServerCertificate())
     {
         Task t1 = Task.Run(() => clientSslStream.AuthenticateAsClient(certificate.GetNameInfo(X509NameType.SimpleName, false)));
         Task t2 = Task.Run(() => serverSslStream.AuthenticateAsServer(certificate));
         return Task.WaitAll(new[] { t1, t2 }, TestConfiguration.PassingTestTimeoutMilliseconds);
     }
 }
开发者ID:dotnet,项目名称:corefx,代码行数:9,代码来源:SslStreamStreamToStreamTest.cs

示例7: TestSyncAdvanced

		public void TestSyncAdvanced()
		{
			IPEndPoint ep = null;
			var evtReady = new AutoResetEvent(false);

			var serverTask = Task.Factory.StartNew(() =>
			{
				var listener = new TcpListener(IPAddress.Loopback, 0);
				listener.Start(5);
				ep = (IPEndPoint)listener.LocalEndpoint;

				evtReady.Set();

				Console.WriteLine("Server> waiting for accept");

				using (var tcp = listener.AcceptTcpClient())
				using (var sslStream = new SslStream(tcp.GetStream(), false, ValidateRemoteCert))
				{
					Console.WriteLine("Server> authenticate");
					sslStream.AuthenticateAsServer(
						_ctx.ServerCertificate,
						true,
						_ctx.CAChain,
						SslProtocols.Tls,
						SslStrength.All,
						true
					);

					Console.WriteLine("Server> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
					Assert.AreEqual("AES256-GCM-SHA384", sslStream.Ssl.CurrentCipher.Name);
					Assert.IsTrue(sslStream.IsMutuallyAuthenticated);

					Console.WriteLine("Server> rx msg");
					var buf = new byte[256];
					sslStream.Read(buf, 0, buf.Length);
					Assert.AreEqual(clientMessage.ToString(), buf.ToString());

					Console.WriteLine("Server> tx msg");
					sslStream.Write(serverMessage, 0, serverMessage.Length);

					Console.WriteLine("Server> done");
				}

				listener.Stop();
			});

			var clientTask = Task.Factory.StartNew(() =>
			{
				evtReady.WaitOne();

				Console.WriteLine("Client> Connecting to: {0}:{1}", ep.Address, ep.Port);

				using (var tcp = new TcpClient(ep.Address.ToString(), ep.Port))
				using (var sslStream = new SslStream(
										   tcp.GetStream(),
										   false,
										   ValidateRemoteCert,
										   SelectClientCertificate))
				{
					Console.WriteLine("Client> authenticate");
					sslStream.AuthenticateAsClient(
						"localhost",
						_ctx.ClientCertificateList,
						_ctx.CAChain,
						SslProtocols.Tls,
						SslStrength.All,
						true
					);

					Console.WriteLine("Client> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
					Assert.AreEqual("AES256-GCM-SHA384", sslStream.Ssl.CurrentCipher.Name);
					Assert.IsTrue(sslStream.IsMutuallyAuthenticated);

					Console.WriteLine("Client> tx msg");
					sslStream.Write(clientMessage, 0, clientMessage.Length);

					Console.WriteLine("Client> rx msg");
					var buf = new byte[256];
					sslStream.Read(buf, 0, buf.Length);
					Assert.AreEqual(serverMessage.ToString(), buf.ToString());

					Console.WriteLine("Client> done");
				}
			});

			Task.WaitAll(clientTask, serverTask);
		}
开发者ID:yaobos,项目名称:openssl-net,代码行数:87,代码来源:TestSSL.cs

示例8: TestSyncIntermediate

		public void TestSyncIntermediate()
		{
			IPEndPoint ep = null;
			var evtReady = new AutoResetEvent(false);

			var serverTask = Task.Factory.StartNew(() =>
			{
				var listener = new TcpListener(IPAddress.Loopback, 0);
				listener.Start(5);
				ep = (IPEndPoint)listener.LocalEndpoint;

				evtReady.Set();

				Console.WriteLine("Server> waiting for accept");

				using (var tcp = listener.AcceptTcpClient())
				using (var sslStream = new SslStream(tcp.GetStream()))
				{
					Console.WriteLine("Server> authenticate");
					sslStream.AuthenticateAsServer(
						_ctx.ServerCertificate,
						false,
						null,
						SslProtocols.Default,
						SslStrength.Low,
						false
					);

					Console.WriteLine("Server> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
					Assert.AreEqual("DES-CBC-SHA", sslStream.Ssl.CurrentCipher.Name);

					Console.WriteLine("Server> rx msg");
					var buf = new byte[256];
					sslStream.Read(buf, 0, buf.Length);
					Assert.AreEqual(clientMessage.ToString(), buf.ToString());

					Console.WriteLine("Server> tx msg");
					sslStream.Write(serverMessage, 0, serverMessage.Length);

					Console.WriteLine("Server> done");
				}

				listener.Stop();
			});

			var clientTask = Task.Factory.StartNew(() =>
			{
				evtReady.WaitOne();

				Console.WriteLine("Client> Connecting to: {0}:{1}", ep.Address, ep.Port);

				using (var tcp = new TcpClient(ep.Address.ToString(), ep.Port))
				using (var sslStream = new SslStream(tcp.GetStream()))
				{
					Console.WriteLine("Client> authenticate");
					sslStream.AuthenticateAsClient(
						"localhost",
						null,
						null,
						SslProtocols.Default,
						SslStrength.Low,
						false
					);

					Console.WriteLine("Client> CurrentCipher: {0}", sslStream.Ssl.CurrentCipher.Name);
					Assert.AreEqual("DES-CBC-SHA", sslStream.Ssl.CurrentCipher.Name);

					Console.WriteLine("Client> tx msg");
					sslStream.Write(clientMessage, 0, clientMessage.Length);

					Console.WriteLine("Client> rx msg");
					var buf = new byte[256];
					sslStream.Read(buf, 0, buf.Length);
					Assert.AreEqual(serverMessage.ToString(), buf.ToString());

					Console.WriteLine("Client> done");
				}
			});

			serverTask.Wait();
			clientTask.Wait();
		}
开发者ID:yaobos,项目名称:openssl-net,代码行数:82,代码来源:TestSSL.cs

示例9: ProcessRequest

        internal void ProcessRequest()
        {
#if(LOG && !MF && !WindowsCE)
            Console.WriteLine((_client.RemoteEndPoint as IPEndPoint).ToString());
#endif

            using (_client)
            {
                while (true)
                {

                    #region Wait for first byte (used for keep-alive, too)

                    int avail = 0;

                    DateTime maxWait = DateTime.Now.AddMilliseconds(2000);
                    do
                    {
                        try
                        {
                            avail = _client.Available;

                            if (avail == 0)
                                Thread.Sleep(10);
                        }
                        catch
                        {
                            break;
                        }
                    }
                    while (avail == 0 && DateTime.Now <= maxWait);

                    #endregion

                    if (avail == 0)
                        break;

                    DateTime begin = DateTime.Now;

                    HttpRequest httpRequest = new HttpRequest();
                    HttpResponse httpResponse = null;

                    Stream stream;

#if(SSL)
                    if (_server.IsSecure && _server.Certificate != null)
                    {
                        SslStream ssl = null;

                        try
                        {
#if(!MF)
                            ssl = new SslStream(new NetworkStream(_client));
                            ssl.AuthenticateAsServer(_server.Certificate, false, SslProtocols.Default, false);
#else
                            ssl = new SslStream(_client);
                            ssl.AuthenticateAsServer(_server.Certificate, SslVerification.NoVerification, SslProtocols.Default);
#endif
                            stream = ssl;
                        }
                        catch (Exception)
                        {
                            Close();
                            return;
                        }
                    }
                    else
#endif
                    {
                        stream = new NetworkStream(_client);
                    }

                    stream.ReadTimeout = 200;
                    stream.WriteTimeout = 1000;

                    try
                    {
                        if (!httpRequest.Read(stream, (_client.RemoteEndPoint as IPEndPoint)))
                        {
                            httpResponse = new HttpResponse();
                            httpResponse.RaiseError(HttpStatusCode.ServiceUnavailable);
                            httpResponse.AddHeader("Connection", "close");
                        }
                    }
                    catch (HttpException ex)
                    {
                        httpResponse = new HttpResponse();
                        httpResponse.RaiseError(ex.Message, ex.Code);
                        httpResponse.AddHeader("Connection", "close");
                    }
                    catch (Exception)
                    {
                        httpResponse = new HttpResponse();
                        httpResponse.RaiseError();
                        httpResponse.AddHeader("Connection", "close");
                    }

                    if (httpResponse == null)
                    {
                        httpResponse = new HttpResponse();
//.........这里部分代码省略.........
开发者ID:RasmusTG,项目名称:HomeAlarmPlusPi,代码行数:101,代码来源:HttpProcessor.cs

示例10: CreateSecureContext

 private void CreateSecureContext(Stream stream, IPEndPoint remoteEndPoint)
 {
     SslStream sslStream = new SslStream(stream, false);
     try
     {
         sslStream.AuthenticateAsServer(_certificate, false, _sslProtocol, false); //todo: this may fail
         new HttpClientContextImp(true, remoteEndPoint, _requestHandler, _disconnectHandler, sslStream,
                                            LogWriter);
     }
     catch (IOException err)
     {
         if (UseTraceLogs)
             _logWriter.Write(this, LogPrio.Trace, err.Message);
     }
     catch (ObjectDisposedException err)
     {
         if (UseTraceLogs)
             _logWriter.Write(this, LogPrio.Trace, err.Message);
     }
 }
开发者ID:kf6kjg,项目名称:halcyon,代码行数:20,代码来源:HttpListener2.cs


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