protected virtual void OnAuthenticationDenied(AuthenticationResult authenticationResult, string user)
 {
     if (PendingAndLoggedInUsers.ContainsKey(authenticationResult.Connection))
     {
         PendingAndLoggedInUsers.Remove(authenticationResult.Connection);
     }
 }
        protected virtual async void ConnectionApproval(NetIncomingMessage msg)
        {
            var connection = msg.SenderConnection;
            var user       = "";
            var password   = "";

            try
            {
                var token = msg.ReadString();
                var tripleDesInformation = await Authenticator.GetKeyFromToken(token);

                ((ServerTripleDesNetEncryptor)NetEncryptor).ImportKeyForConnection(msg.SenderConnection, tripleDesInformation.Key, tripleDesInformation.Iv);
                ((ServerTripleDesNetEncryptor)NetEncryptor).Decrypt(msg);

                user     = msg.ReadString().ToLower();
                password = msg.ReadString();
            }
            catch (Exception e)
            {
                Log(e);
                connection.Deny(NetConnectionResult.Unknown);
                NetConnector.Recycle(msg);
                return;
            }

            if (PendingAndLoggedInUsers.Values.Any(c => c.UserName == user))
            {
                AuthenticationResults.Add(new Tuple <AuthenticationResult, string>(
                                              new AuthenticationResult()
                {
                    Connection   = connection,
                    Success      = false,
                    RequestState = RequestState.UserAlreadyLoggedIn
                }, user));
                return;
            }
            PendingAndLoggedInUsers.Add(connection, new CommunicationUser <TAuthenticationUser>()
            {
                UserName = user
            });
            var authTask = Authenticator.Authenticate(user, password)
                           .ContinueWith(async approval =>
            {
                var authentication        = await approval;
                authentication.Connection = connection;

                ConnectionApprovalExtras(authentication);
                if (authentication.Success && !string.IsNullOrEmpty(authentication.UserId))
                {
                    var userData = await GetUser(authentication.UserId);
                    PendingAndLoggedInUsers[connection].UserData     = userData;
                    PendingAndLoggedInUsers[connection].LoggedInTime = DateTime.UtcNow;
                }
                AuthenticationResults.Add(new Tuple <AuthenticationResult, string>(authentication, user));
                NetConnector.Recycle(msg);
            });

            AddRunningTask(authTask);
        }
        protected void DoForUsers(Func <TAuthenticationUser, bool> predicate, Action <NetConnection, TAuthenticationUser> onConnectionAction)
        {
            var users = PendingAndLoggedInUsers.ToList();

            foreach (var kv in users)
            {
                if (kv.Value.UserData == null) // user pending a log in - ignored...
                {
                    continue;
                }
                if (predicate(kv.Value.UserData))
                {
                    onConnectionAction(kv.Key, kv.Value.UserData);
                }
            }
        }
 protected virtual bool AuthorizedForMessage(NetConnection connection)
 {
     return(PendingAndLoggedInUsers.ContainsKey(connection));
 }
 protected override void OnDisconnected_Internal(NetConnection connection)
 {
     OnUserDisconnected(PendingAndLoggedInUsers[connection]);
     PendingAndLoggedInUsers.Remove(connection);
     ((ServerTripleDesNetEncryptor)NetEncryptor).ConnectionCryptoProviders.Remove(connection);
 }