private WindowsIdentity Authenticate(ref Stream netStream, TcpServerSocketHandler streamManager)
        {
            // Use the identity for impersonation etc.
            NegotiateStream negoServer = null;

            try
            {
                negoServer = new NegotiateStream(netStream);
                // Block for authentication request
                TokenImpersonationLevel impLevel = TokenImpersonationLevel.Identification;
                if (_impersonate)
                {
                    impLevel = TokenImpersonationLevel.Impersonation;
                }
                negoServer.AuthenticateAsServer((NetworkCredential)CredentialCache.DefaultCredentials, _protectionLevel, impLevel);
                netStream = negoServer;
                return((WindowsIdentity)negoServer.RemoteIdentity);
            }
            catch
            {
                streamManager.SendErrorResponse(
                    String.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Tcp_ServerAuthenticationFailed")), false);
                if (negoServer != null)
                {
                    negoServer.Close();
                }
                throw;
            }
        }
        private WindowsIdentity Authenticate(ref Stream netStream, TcpServerSocketHandler streamManager)
        {
            NegotiateStream stream = null;
            WindowsIdentity remoteIdentity;

            try
            {
                stream = new NegotiateStream(netStream);
                TokenImpersonationLevel identification = TokenImpersonationLevel.Identification;
                if (this._impersonate)
                {
                    identification = TokenImpersonationLevel.Impersonation;
                }
                stream.AuthenticateAsServer((NetworkCredential)CredentialCache.DefaultCredentials, this._protectionLevel, identification);
                netStream      = stream;
                remoteIdentity = (WindowsIdentity)stream.RemoteIdentity;
            }
            catch
            {
                streamManager.SendErrorResponse(string.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Tcp_ServerAuthenticationFailed"), new object[0]), false);
                if (stream != null)
                {
                    stream.Close();
                }
                throw;
            }
            return(remoteIdentity);
        }
 private WindowsIdentity Authenticate(ref Stream netStream, TcpServerSocketHandler streamManager)
 { 
     // Use the identity for impersonation etc. 
     NegotiateStream negoServer = null;
     try 
     {
         negoServer = new NegotiateStream(netStream);
         // Block for authentication request
         TokenImpersonationLevel impLevel = TokenImpersonationLevel.Identification; 
         if (_impersonate)
             impLevel = TokenImpersonationLevel.Impersonation; 
         negoServer.AuthenticateAsServer((NetworkCredential)CredentialCache.DefaultCredentials, _protectionLevel, impLevel); 
         netStream = negoServer;
         return (WindowsIdentity)negoServer.RemoteIdentity; 
     }
     catch
     {
         streamManager.SendErrorResponse( 
             String.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Tcp_ServerAuthenticationFailed")), false);
         if (negoServer != null) 
             negoServer.Close(); 
         throw;
     } 
 }
        // AcceptSocket method which will invoke the
        // authorization callbacks
        void AcceptSocketCallback(IAsyncResult ar)
        {
            Socket socket = null;

            InternalRemotingServices.RemotingTrace("TCPChannel::Listen - tcpListen.Pending() == true");
            TcpServerSocketHandler streamManager = null;
            bool closeImmediately = true;

            try
            {
                //
                // Wait for an incoming socket
                // if the listener is still active
                if (_tcpListener.IsListening)
                {
                    _tcpListener.BeginAcceptSocket(_acceptSocketCallback, null);
                }

                socket = _tcpListener.EndAcceptSocket(ar);

                if (socket == null)
                {
                    throw new RemotingException(
                              String.Format(
                                  CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Socket_Accept"),
                                  Marshal.GetLastWin32Error().ToString(CultureInfo.CurrentCulture)));
                }

                if (_authorizeRemotingConnection != null)
                {
                    bool authorized = _authorizeRemotingConnection.IsConnectingEndPointAuthorized(socket.RemoteEndPoint);
                    if (!authorized)
                    {
                        throw new RemotingException(CoreChannel.GetResourceString(
                                                        "Remoting_Tcp_ServerAuthorizationEndpointFailed"));
                    }
                }

                // disable nagle delay
                socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
                // Set keepalive flag, so that inactive sockets can be cleaned up
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);

                // set linger option
                LingerOption lingerOption = new LingerOption(true, 3);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);

                Stream netStream = new SocketStream(socket);
                streamManager = new TcpServerSocketHandler(socket, CoreChannel.RequestQueue, netStream);

                // If authentication is requested wait for auth request.
                closeImmediately = false;
                if (_secure)
                {
                    throw new NotSupportedException();
                }



                streamManager.DataArrivedCallback = new WaitCallback(_transportSink.ServiceRequest);
                streamManager.BeginReadMessage();
            }
            catch (Exception e)
            {
                // Close the socket pre-emptively. We also close the socket if
                // We need to catch all exceptions if we hit ObjectDisposedException
                try{
                    if (streamManager != null)
                    {
                        streamManager.SendErrorResponse(e, false);
                    }
                    if (socket != null)
                    {
                        if (closeImmediately)
                        {
                            socket.Close(0);
                        }
                        else
                        {
                            socket.Close();
                        }
                    }
                }catch (Exception) {}
                if (!_bListening)
                {
                    // We called Stop() on the tcp listener, so gracefully exit.
                    //bOkToListen = false;
                }
                else
                {
                    // we want the exception to show up as unhandled since this
                    //   is an unexpected failure.
                    if (!(e is SocketException))
                    {
                        //throw;
                    }
                }
            }
        }
        // AcceptSocket method which will invoke the
        // authorization callbacks
        void AcceptSocketCallback(IAsyncResult ar)
        {
            Socket socket = null;
            InternalRemotingServices.RemotingTrace("TCPChannel::Listen - tcpListen.Pending() == true");
            TcpServerSocketHandler streamManager = null;
            bool closeImmediately = true;
            try
            {
                //
                // Wait for an incoming socket
                // if the listener is still active
                if (_tcpListener.IsListening)
                    _tcpListener.BeginAcceptSocket(_acceptSocketCallback, null);

                socket = _tcpListener.EndAcceptSocket(ar);

                if (socket == null)
                {
                    throw new RemotingException(
                        String.Format(
                            CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Socket_Accept"),
                            Marshal.GetLastWin32Error().ToString(CultureInfo.CurrentCulture)));
                }

                if (_authorizeRemotingConnection != null)
                {
                        bool authorized = _authorizeRemotingConnection.IsConnectingEndPointAuthorized(socket.RemoteEndPoint);
                        if (!authorized)
                            throw new RemotingException(CoreChannel.GetResourceString(
                                                                "Remoting_Tcp_ServerAuthorizationEndpointFailed"));
                }                        

                // disable nagle delay
                socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
                // Set keepalive flag, so that inactive sockets can be cleaned up
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);

                // set linger option
                LingerOption lingerOption = new LingerOption(true, 3);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);

                Stream netStream = new SocketStream(socket);
                streamManager = new TcpServerSocketHandler(socket, CoreChannel.RequestQueue, netStream);

                // If authentication is requested wait for auth request.
                closeImmediately = false;
                if (_secure)
                {
                    throw new NotSupportedException();
                }



                streamManager.DataArrivedCallback = new WaitCallback(_transportSink.ServiceRequest);
                streamManager.BeginReadMessage();
            }
            catch (Exception e)
            {
                // Close the socket pre-emptively. We also close the socket if
                // We need to catch all exceptions if we hit ObjectDisposedException
                try{
                    if (streamManager != null){
                        streamManager.SendErrorResponse(e, false);
                    }
                    if (socket != null){
                        if (closeImmediately)
                             socket.Close(0);
                        else
                            socket.Close();
                    }
                }catch(Exception){}
                if (!_bListening)
                {
                    // We called Stop() on the tcp listener, so gracefully exit.
                    //bOkToListen = false;
                }
                else
                {
                    // we want the exception to show up as unhandled since this
                    //   is an unexpected failure.
                    if (!(e is SocketException))
                    {
                        //throw;
                    }
                }
            }
        }
 private WindowsIdentity Authenticate(ref Stream netStream, TcpServerSocketHandler streamManager)
 {
     NegotiateStream stream = null;
     WindowsIdentity remoteIdentity;
     try
     {
         stream = new NegotiateStream(netStream);
         TokenImpersonationLevel identification = TokenImpersonationLevel.Identification;
         if (this._impersonate)
         {
             identification = TokenImpersonationLevel.Impersonation;
         }
         stream.AuthenticateAsServer((NetworkCredential) CredentialCache.DefaultCredentials, this._protectionLevel, identification);
         netStream = stream;
         remoteIdentity = (WindowsIdentity) stream.RemoteIdentity;
     }
     catch
     {
         streamManager.SendErrorResponse(string.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Tcp_ServerAuthenticationFailed"), new object[0]), false);
         if (stream != null)
         {
             stream.Close();
         }
         throw;
     }
     return remoteIdentity;
 }