Exemplo n.º 1
0
        protected void LoginRequest(PacketHeader header, Connection connection, LoginRequest account)
        {
            string sessionId = Guid.NewGuid().ToString();
            long   accountId;
            string passwordHash;

            if (accountRepository.AccountPasswordHash(account.username, out accountId, out passwordHash))
            {
                if (Security.PasswordHasher.VerifyHashedPassword(passwordHash, account.password))
                {
                    if (account.admin)
                    {
                        if (groupRepository.Contains(0, accountId))
                        {
                            // Authorized...

                            accountRepository.SetSession(accountId, sessionId);

                            lock (authorizedAccounts)
                            {
                                if (authorizedAccounts.ContainsKey(connection.ConnectionInfo.NetworkIdentifier))
                                {
                                    authorizedAccounts[connection.ConnectionInfo.NetworkIdentifier] = accountId;
                                }
                                else
                                {
                                    authorizedAccounts.Add(connection.ConnectionInfo.NetworkIdentifier, accountId);
                                }
                            }

                            TCPConnection.GetConnection(connection.ConnectionInfo).
                            SendObject(PacketName.ReLoginResult.ToString(),
                                       (int)LoginResponse.Success);
                            TCPConnection.GetConnection(connection.ConnectionInfo).
                            SendObject(PacketName.ReSessionId.ToString(), sessionId);
                        }
                        else
                        {
                            TCPConnection.GetConnection(connection.ConnectionInfo).
                            SendObject(PacketName.ReLoginResult.ToString(),
                                       (int)LoginResponse.NoAdminAccess);
                        }
                    }
                    else
                    {
                        // Authorized...
                        accountRepository.SetSession(accountId, sessionId);

                        lock (authorizedAccounts)
                        {
                            if (authorizedAccounts.ContainsKey(connection.ConnectionInfo.NetworkIdentifier))
                            {
                                authorizedAccounts[connection.ConnectionInfo.NetworkIdentifier] = accountId;
                            }
                            else
                            {
                                authorizedAccounts.Add(connection.ConnectionInfo.NetworkIdentifier, accountId);
                            }
                        }

                        TCPConnection.GetConnection(connection.ConnectionInfo).
                        SendObject(PacketName.ReLoginResult.ToString(), (int)LoginResponse.Success);
                        TCPConnection.GetConnection(connection.ConnectionInfo).
                        SendObject(PacketName.ReSessionId.ToString(), sessionId);
                    }
                }
                else
                {
                    // Wrong password
                    TCPConnection.GetConnection(connection.ConnectionInfo).
                    SendObject(PacketName.ReLoginResult.ToString(),
                               (int)LoginResponse.WrongUsernamePassword);
                }
            }
            else
            {
                // Account doesn't exist...
                TCPConnection.GetConnection(connection.ConnectionInfo).
                SendObject(PacketName.ReLoginResult.ToString(),
                           (int)LoginResponse.WrongUsernamePassword);
            }
        }