Exemplo n.º 1
0
        public static void AuthenticateClient(TcpClient client)
        {
            var networkStream = client.GetStream();
            // Create the NegotiateStream.
            var negotiateStream = new NegotiateStream(networkStream, false);
            // Combine client and NegotiateStream instance into ClientState.
            var clientState = new ClientState(negotiateStream, client);

            // Listen for the client authentication request.
            negotiateStream.BeginAuthenticateAsServer(
                EndAuthenticateCallback,
                clientState
                );

            // Wait until the authentication completes.
            clientState.Waiter.WaitOne();
            clientState.Waiter.Reset();

            // Receive encoded message sent by client.
            negotiateStream.BeginRead(
                clientState.Buffer,
                0,
                clientState.Buffer.Length,
                EndReadCallback,
                clientState);
            clientState.Waiter.WaitOne();

            // Close stream and client.
            negotiateStream.Close();
            client.Close();
        }
Exemplo n.º 2
0
        //<snippet1>
        public static void AuthenticateClient(TcpClient clientRequest)
        {
            NetworkStream stream = clientRequest.GetStream();
            // Create the NegotiateStream.
            NegotiateStream authStream = new NegotiateStream(stream, false);
            // Save the current client and NegotiateStream instance
            // in a ClientState object.
            ClientState cState = new ClientState(authStream, clientRequest);

            // Listen for the client authentication request.
            authStream.BeginAuthenticateAsServer(
                new AsyncCallback(EndAuthenticateCallback),
                cState
                );
            // Wait until the authentication completes.
            cState.Waiter.WaitOne();
            cState.Waiter.Reset();
            authStream.BeginRead(cState.Buffer, 0, cState.Buffer.Length,
                                 new AsyncCallback(EndReadCallback),
                                 cState);
            cState.Waiter.WaitOne();
            // Finished with the current client.
            authStream.Close();
            clientRequest.Close();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Extends BeginAuthenticateAsServer so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// negotiatestream.BeginAuthenticateAsServer(credential, policy, requiredProtectionLevel, requiredImpersonationLevel, asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginAuthenticateAsServer(this NegotiateStream negotiatestream, System.Net.NetworkCredential credential, System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicy policy, ProtectionLevel requiredProtectionLevel, System.Security.Principal.TokenImpersonationLevel requiredImpersonationLevel, AsyncCallback asyncCallback)
        {
            if (negotiatestream == null)
            {
                throw new ArgumentNullException("negotiatestream");
            }

            return(negotiatestream.BeginAuthenticateAsServer(credential, policy, requiredProtectionLevel, requiredImpersonationLevel, asyncCallback, null));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Extends BeginAuthenticateAsServer so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// negotiatestream.BeginAuthenticateAsServer(policy, asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginAuthenticateAsServer(this NegotiateStream negotiatestream, System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicy policy, AsyncCallback asyncCallback)
        {
            if (negotiatestream == null)
            {
                throw new ArgumentNullException("negotiatestream");
            }

            return(negotiatestream.BeginAuthenticateAsServer(policy, asyncCallback, null));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Extends BeginAuthenticateAsServer so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// negotiatestream.BeginAuthenticateAsServer(asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginAuthenticateAsServer(this NegotiateStream negotiatestream, AsyncCallback asyncCallback)
        {
            if (negotiatestream == null)
            {
                throw new ArgumentNullException("negotiatestream");
            }

            return(negotiatestream.BeginAuthenticateAsServer(asyncCallback, null));
        }
Exemplo n.º 6
0
 protected override Task AuthenticateAsServerAsync(NegotiateStream server) =>
 Task.Factory.FromAsync(
     (callback, state) => server.BeginAuthenticateAsServer((NetworkCredential)CredentialCache.DefaultCredentials, ProtectionLevel.EncryptAndSign, TokenImpersonationLevel.Identification, callback, state),
     server.EndAuthenticateAsServer, null);