Exemplo n.º 1
0
        internal void ReceiveKerberos(Object threadContext)
        {
            try
            {
                _AuthStream.BeginRead(_buff, 0, _buff.Length, myReceiveKerberosCallback, _AuthStream);
            }
            catch
            {
                StreamIsConnected = false;
                Stop();

                if (_rbr2 != null)
                {
                    _rbr2.Close();
                }
                if (_rbr2 != null)
                {
                    _rbr2 = null;
                }
                if (_rms2 != null)
                {
                    _rms2.Dispose();
                }
                if (_buff != null)
                {
                    _buff = null;
                }
            }
        }
Exemplo n.º 2
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.º 3
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.º 4
0
        /// <summary>
        /// Extends BeginRead so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// negotiatestream.BeginRead(buffer, offset, count, asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginRead(this NegotiateStream negotiatestream, Byte[] buffer, Int32 offset, Int32 count, AsyncCallback asyncCallback)
        {
            if (negotiatestream == null)
            {
                throw new ArgumentNullException("negotiatestream");
            }

            return(negotiatestream.BeginRead(buffer, offset, count, asyncCallback, null));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Extends BeginRead so that buffer offset of 0 and call to Array.Length are not needed.
        /// <example>
        /// negotiatestream.BeginRead(buffer, asyncCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginRead(this NegotiateStream negotiatestream, Byte[] buffer, AsyncCallback asyncCallback)
        {
            if (negotiatestream == null)
            {
                throw new ArgumentNullException("negotiatestream");
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            return(negotiatestream.BeginRead(buffer, 0, buffer.Length, asyncCallback));
        }
Exemplo n.º 6
0
        public async Task NegotiateStream_ConcurrentAsyncReadOrWrite_ThrowsNotSupportedException()
        {
            byte[] recvBuf = new byte[s_sampleMsg.Length];
            var    network = new VirtualNetwork();

            using (var clientStream = new VirtualNetworkStream(network, isServer: false))
                using (var serverStream = new VirtualNetworkStream(network, isServer: true))
                    using (var client = new NegotiateStream(clientStream))
                        using (var server = new NegotiateStream(serverStream))
                        {
                            await TestConfiguration.WhenAllOrAnyFailedWithTimeout(
                                client.AuthenticateAsClientAsync(CredentialCache.DefaultNetworkCredentials, string.Empty),
                                server.AuthenticateAsServerAsync());

                            // Custom EndWrite/Read will not reset the variable which monitors concurrent write/read.
                            await TestConfiguration.WhenAllOrAnyFailedWithTimeout(
                                Task.Factory.FromAsync(client.BeginWrite, (ar) => { Assert.NotNull(ar); }, s_sampleMsg, 0, s_sampleMsg.Length, client),
                                Task.Factory.FromAsync(server.BeginRead, (ar) => { Assert.NotNull(ar); }, recvBuf, 0, s_sampleMsg.Length, server));

                            Assert.Throws <NotSupportedException>(() => client.BeginWrite(s_sampleMsg, 0, s_sampleMsg.Length, (ar) => { Assert.Null(ar); }, null));
                            Assert.Throws <NotSupportedException>(() => server.BeginRead(recvBuf, 0, s_sampleMsg.Length, (ar) => { Assert.Null(ar); }, null));
                        }
        }
Exemplo n.º 7
0
//</snippet2>
//<snippet3>
        public static void EndReadCallback(IAsyncResult ar)
        {
            // Get the saved data.
            ClientState     cState        = (ClientState)ar.AsyncState;
            TcpClient       clientRequest = cState.Client;
            NegotiateStream authStream    = (NegotiateStream)cState.AuthenticatedStream;
            // Get the buffer that stores the message sent by the client.
            int bytes = -1;

            // Read the client message.
            try
            {
                bytes = authStream.EndRead(ar);
                cState.Message.Append(Encoding.UTF8.GetChars(cState.Buffer, 0, bytes));
                if (bytes != 0)
                {
                    authStream.BeginRead(cState.Buffer, 0, cState.Buffer.Length,
                                         new AsyncCallback(EndReadCallback),
                                         cState);
                    return;
                }
            }
            catch (Exception e)
            {
                // A real application should do something
                // useful here, such as logging the failure.
                Console.WriteLine("Client message exception:");
                Console.WriteLine(e);
                cState.Waiter.Set();
                return;
            }
            IIdentity id = authStream.RemoteIdentity;

            Console.WriteLine("{0} says {1}", id.Name, cState.Message.ToString());
            cState.Waiter.Set();
        }