Exemplo n.º 1
0
        public ServerResponse ChangePassword(string userid, string previousPassword, string newPassword)
        {
            ServerResponse response = ServerResponse.ServerError;

            try
            {
                ObjectId userObjId = ObjectId.Parse(userid);
                User     user      = UserManager.Instance.FindUserByID(userObjId);
                if (Authorize.PassesGuidelines(newPassword))
                {
                    byte[] passBytes = Encoding.UTF8.GetBytes(newPassword);

                    bool passwordNotUsedBefore = user.ChangePassword(passBytes);
                    if (passwordNotUsedBefore)
                    {
                        UserManager.Instance.SaveUser(user);
                        response = ServerResponse.Success;
                    }
                    else
                    {
                        response = ServerResponse.InvalidPassword;
                    }
                }
                else
                {
                    response = ServerResponse.InvalidPasswordType;
                }
            }
            catch (Exception ex)
            {
                WriteLog(ex);
                response = ServerResponse.ServerError;
            }
            return(response);
        }
Exemplo n.º 2
0
        public Tuple <ServerResponse, ObjectId> CreateNewAccount(string username, string password)
        {
            Tuple <ServerResponse, ObjectId> response;

            try
            {
                if (Authorize.PassesGuidelines(password))
                {
                    byte[]   passBytes  = Encoding.UTF8.GetBytes(password);
                    byte[]   salt       = Authorize.GenerateSalt();
                    byte[]   saltedHash = Authorize.GenerateSaltedHash(passBytes, salt);
                    Password pass       = new Password(saltedHash, salt);
                    User     newUser    = new User(username.ToLower(), pass);
                    try
                    {
                        UserManager.Instance.SaveUser(newUser);
                        response = new Tuple <ServerResponse, ObjectId>(ServerResponse.Success, newUser.id);
                    }
                    catch (MongoWriteConcernException ex)
                    {
                        WriteLog(ex);
                        response = new Tuple <ServerResponse, ObjectId>(ServerResponse.UsernameAlreadyExists, ObjectId.Empty);
                    }
                }
                else
                {
                    response = new Tuple <ServerResponse, ObjectId>(ServerResponse.UsernameAlreadyExists, ObjectId.Empty);
                }
            }
            catch (Exception ex)
            {
                WriteLog(ex);
                response = new Tuple <ServerResponse, ObjectId>(ServerResponse.ServerError, ObjectId.Empty);
            }
            return(response);
        }