public override Sys.MembershipUser GetUser(object providerUserKey, bool userIsOnline)
 {
     if (!(providerUserKey is Guid)) throw new InvalidOperationException("No Guid providerUserKey");
     var uid = (Guid)providerUserKey;
     using (var db = new Context()) {
         var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserId == uid);
         if (user != null) {
             if (userIsOnline) {
                 user.LastActivityDate = DateTime.UtcNow;
                 db.SaveChanges();
             }
             return user.MembershipUser;
         }
     }
     return null;
 }
 public override System.Guid GetUserIdFromPasswordResetToken(string token)
 {
     if (string.IsNullOrEmpty(token)) {
         throw CreateArgumentNullOrEmptyException("token");
     }
     using (var db = new Context()) {
         var result = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.PasswordVerificationToken == token);
         if (result != null) {
             return result.UserId;
         }
         return Guid.Empty;
     }
 }
 public override bool ConfirmAccount(string accountConfirmationToken)
 {
     if (string.IsNullOrEmpty(accountConfirmationToken)) {
         throw CreateArgumentNullOrEmptyException("accountConfirmationToken");
     }
     using (var db = new Context()) {
         try {
             var row = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.ConfirmationToken == accountConfirmationToken);
             if (row != null) {
                 row.IsConfirmed = true;
                 // row.LastActivityDate = DateTime.UtcNow;
                 db.SaveChanges();
                 return true;
             }
         } catch { }
         return false;
     }
 }
 public override System.Web.Security.MembershipUser GetUser(string userName, bool userIsOnline)
 {
     if (string.IsNullOrEmpty(userName)) {
         // throw CreateArgumentNullOrEmptyException("userName");
         return null;
     }
     using (var db = new Context()) {
         var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == userName);
         if (user == null) {
             return null;
         }
         if (userIsOnline) {
             user.LastActivityDate = DateTime.UtcNow;
             db.SaveChanges();
         }
         return user.MembershipUser;
     }
 }
 public override bool UnlockUser(string username)
 {
     if (string.IsNullOrEmpty(username)) throw CreateArgumentNullOrEmptyException("username");
     using (var db = new Context()) {
         var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == username);
         if (user != null && user.IsLockedOut) {
             user.IsLockedOut = false;
             db.SaveChanges();
             return true;
         }
     }
     return false;
 }
 public override bool ValidateUser(string username, string password)
 {
     if (string.IsNullOrEmpty(username)) throw CreateArgumentNullOrEmptyException("username");
     using (var db = new Context()) {
         var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == username);
         if (user != null) {
             user.LastActivityDate = DateTime.UtcNow;
             db.SaveChanges();
             return user.CheckPassword(password);
         }
     }
     return false;
 }
        public override string GeneratePasswordResetToken(string userName, int tokenExpirationInMinutesFromNow)
        {
            if (string.IsNullOrEmpty(userName)) {
                throw CreateArgumentNullOrEmptyException("userName");
            }
            using (var db = new Context()) {
                try {
                    var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == userName);
                    if (user == null) {
                        throw new InvalidOperationException(string.Format("User not found: {0}", userName));
                    }
                    if (!user.IsConfirmed) {
                        throw new InvalidOperationException(string.Format("User not found: {0}", userName));
                    }
                    string token = null;
                    if (user.PasswordVerificationTokenExpirationDate > DateTime.UtcNow) {
                        token = user.PasswordVerificationToken;
                    } else {
                        token = Services.Crypto.GenerateToken();
                    }
                    user.PasswordVerificationToken = token;
                    user.PasswordVerificationTokenExpirationDate = DateTime.UtcNow.AddMinutes(tokenExpirationInMinutesFromNow);

                    // user.LastActivityDate = DateTime.UtcNow;
                    db.SaveChanges();
                    return token;
                } catch {
                    throw new InvalidOperationException(string.Format("User not found: {0}", userName));
                }
            }
        }
 public override string ResetPassword(string username, string answer)
 {
     if (string.IsNullOrEmpty(username)) throw CreateArgumentNullOrEmptyException("username");
     using (var db = new Context()) {
         var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == username);
         if (user != null) {
             if (user.CheckAnswer(answer)) {
                 var pwd = GeneratePassword();
                 user.SetPassword(this, pwd);
                 return pwd;
             }
         }
     }
     return null;
 }
 public override Sys.MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
 {
     using (var db = new Context()) {
         var users = db.AppActiveUsers<MembershipProvider>().Where(u => u.Email == emailToMatch).Skip(pageIndex).Take(pageSize);
         totalRecords = users.Count();
         var collection = new Sys.MembershipUserCollection();
         foreach (var u in users) collection.Add(u.MembershipUser);
         return collection;
     }
 }
 public override Sys.MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
 {
     using (var db = new Context()) {
         IEnumerable<User> users;
         if (usernameToMatch.ContainsAny(',', ';', '*')) {
             users = db.AppActiveUsers<MembershipProvider>().AsEnumerable().Where(u => Services.Paths.Match(usernameToMatch, u.UserName));
         } else {
             users = db.AppActiveUsers<MembershipProvider>().Where(u => u.UserName == usernameToMatch);
         }
         users = users.Skip(pageIndex).Take(pageSize);
         totalRecords = users.Count();
         var collection = new Sys.MembershipUserCollection();
         foreach (var u in users) collection.Add(u.MembershipUser);
         return collection;
     }
 }
        public override string ExtendedValidateUser(string userNameOrEmail, string password)
        {
            if (string.IsNullOrEmpty(userNameOrEmail)) {
                throw CreateArgumentNullOrEmptyException("userNameOrEmail");
            }
            if (string.IsNullOrEmpty(password)) {
                throw CreateArgumentNullOrEmptyException("password");
            }
            using (var db = new Context()) {
                try {
                    User user = null;
                    user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == userNameOrEmail);
                    if (user == null) {
                        user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.Email == userNameOrEmail);
                    }
                    if (user == null) {
                        return string.Empty;
                    }
                    if (!user.IsConfirmed || !user.IsApproved) {
                        return string.Empty;
                    }

                    bool verificationSucceeded = user.CheckPassword(password);

                    user.LastActivityDate = DateTime.UtcNow;
                    db.SaveChanges();
                    if (verificationSucceeded) {
                        return user.UserName;
                    }
                } catch { }
                return string.Empty;
            }
        }
        public override Sys.MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out Sys.MembershipCreateStatus status)
        {
            status = Sys.MembershipCreateStatus.Success;

            if (string.IsNullOrEmpty(username)) status = Sys.MembershipCreateStatus.InvalidUserName;
            //if (string.IsNullOrEmpty(email)) status = Sys.MembershipCreateStatus.InvalidEmail;
            if (!ValidPassword(password)) status = Sys.MembershipCreateStatus.InvalidPassword;
            if (RequiresQuestionAndAnswer) {
                if (string.IsNullOrEmpty(passwordQuestion)) status = Sys.MembershipCreateStatus.InvalidQuestion;
                if (string.IsNullOrEmpty(passwordAnswer)) status = Sys.MembershipCreateStatus.InvalidAnswer;
            }

            if (status != Sys.MembershipCreateStatus.Success) return null;

            using (var db = new Context()) {

                try {
                    var user = db.AppUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == username);

                    if (user != null && user.IsDeleted) {
                        db.Users.Remove(user);
                        user = null;
                    }

                    User emailuser = null;
                    if (!string.IsNullOrEmpty(email)) {
                        emailuser = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.Email == email);
                    }
                    if (user != null) status = Sys.MembershipCreateStatus.DuplicateUserName;
                    if (emailuser != null && RequiresUniqueEmail) status = Sys.MembershipCreateStatus.DuplicateEmail;
                    if (status != Sys.MembershipCreateStatus.Success) return null;

                    User NewUser = new User {
                        UserId = Guid.NewGuid(),
                        UserName = username,
                        IsConfirmed = true,
                        Email = email,
                        ConfirmationToken = null,
                        CreationDate = DateTime.UtcNow,
                        PasswordChangedDate = DateTime.UtcNow,
                        PasswordFailuresSinceLastSuccess = 0,
                        AnswerFailuresSinceLastSuccess = 0,
                        LastPasswordFailureDate = null,
                        LastActivityDate = DateTime.UtcNow,
                        LastLoginDate = null,
                        LastLockoutDate = null,
                        PasswordQuestion = passwordQuestion,
                        PasswordAnswer = passwordAnswer,
                        IsApproved = isApproved,
                        Application = Application.Current<MembershipProvider>(db)
                    };
                    if (providerUserKey is Guid) NewUser.UserId = (Guid)providerUserKey;
                    else NewUser.UserId = Guid.NewGuid();

                    NewUser.SetPassword(this, password);

                    db.Users.Add(NewUser);
                    db.SaveChanges();
                    return NewUser.MembershipUser;
                } catch (Exception ex) {
                    status = Sys.MembershipCreateStatus.DuplicateUserName; return null;
                }
            }
        }
        public override string CreateAccount(string userName, string password, string email, bool requireConfirmationToken)
        {
            if (string.IsNullOrEmpty(userName)) {
                throw new Sys.MembershipCreateUserException(Sys.MembershipCreateStatus.InvalidUserName);
            }
            /*if (string.IsNullOrEmpty(email)) {
                throw new Sys.MembershipCreateUserException(Sys.MembershipCreateStatus.InvalidEmail);
            }*/
            if (!ValidPassword(password)) throw new Sys.MembershipCreateUserException(Sys.MembershipCreateStatus.InvalidPassword);

            using (var db = new Context()) {
                try {
                    var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == userName);

                    if (user != null && user.IsDeleted) {
                        db.Users.Remove(user);
                        user = null;
                    }

                    User emailuser = null;
                    if (!string.IsNullOrEmpty(email)) {
                        emailuser = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.Email == email);
                    }
                    if (user != null) {
                        throw new Sys.MembershipCreateUserException(Sys.MembershipCreateStatus.DuplicateUserName);
                    }
                    if (emailuser != null && RequiresUniqueEmail) {
                        throw new Sys.MembershipCreateUserException(Sys.MembershipCreateStatus.DuplicateEmail);
                    }
                    string token = null;
                    if (requireConfirmationToken) {
                        token = Services.Crypto.GenerateToken();
                    }

                    User NewUser = new User {
                        UserId = Guid.NewGuid(),
                        UserName = userName,
                        Application = Application.Current<MembershipProvider>(db),
                        IsConfirmed = !requireConfirmationToken,
                        IsApproved = true,
                        Email = email,
                        ConfirmationToken = token,
                        CreationDate = DateTime.UtcNow,
                        PasswordChangedDate = DateTime.UtcNow,
                        PasswordFailuresSinceLastSuccess = 0,
                        AnswerFailuresSinceLastSuccess = 0,
                        LastPasswordFailureDate = null,
                        LastAnswerFailureDate = null,
                        LastActivityDate = DateTime.UtcNow,
                        LastLoginDate = null,
                        LastLockoutDate = null,
                        Comment = null
                    };

                    NewUser.SetPassword(this, password);

                    db.Users.Add(NewUser);
                    db.SaveChanges();
                    return token;
                } catch {
                    throw new Sys.MembershipCreateUserException(Sys.MembershipCreateStatus.DuplicateUserName);
                }
            }
        }
 public override string GetUserNameByEmail(string email)
 {
     if (string.IsNullOrEmpty(email)) throw CreateArgumentNullOrEmptyException("email");
     using (var db = new Context()) {
         var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.Email == email);
         if (user != null) return user.UserName;
     }
     return null;
 }
 public override Sys.MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
 {
     using (var db = new Context()) {
         var users = db.AppActiveUsers<MembershipProvider>().OrderBy(u => u.UserName).Skip(pageIndex).Take(pageSize);
         totalRecords = users.Count();
         var collection = new Sys.MembershipUserCollection();
         foreach (var u in users) collection.Add(u.MembershipUser);
         return collection;
     }
 }
        public override bool IsConfirmed(string userName)
        {
            if (string.IsNullOrEmpty(userName)) {
                throw CreateArgumentNullOrEmptyException("userName");
            }
            using (var db = new Context()) {
                var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == userName);
                if (user == null) {
                    return false;
                }

                // user.LastActivityDate = DateTime.UtcNow;

                if (user.IsConfirmed) {
                    return true;
                } else {
                    return false;
                }
            }
        }
 public override System.DateTime GetLastPasswordFailureDate(string userName)
 {
     if (string.IsNullOrEmpty(userName)) {
         throw CreateArgumentNullOrEmptyException("userName");
     }
     using (var db = new Context()) {
         var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == userName);
         if (user == null) {
             throw new InvalidOperationException(string.Format("User not found: {0}", userName));
         }
         return user.LastPasswordFailureDate ?? DateTime.MinValue;
     }
 }
 public override bool ResetPasswordWithToken(string token, string newPassword)
 {
     if (string.IsNullOrEmpty(newPassword)) {
         throw CreateArgumentNullOrEmptyException("newPassword");
     }
     using (var db = new Context()) {
         try {
             var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.PasswordVerificationToken == token && u.PasswordVerificationTokenExpirationDate > DateTime.UtcNow);
             if (user != null) {
                 user.SetPassword(this, newPassword);
                 user.PasswordChangedDate = DateTime.UtcNow;
                 user.PasswordVerificationToken = null;
                 user.PasswordVerificationTokenExpirationDate = null;
                 // user.LastActivityDate = DateTime.UtcNow;
                 db.SaveChanges();
                 return true;
             }
         } catch { }
         return false;
     }
 }
 public override int GetNumberOfUsersOnline()
 {
     using (var db = new Context()) {
         var now = DateTime.UtcNow;
         var window = Sys.Membership.UserIsOnlineTimeWindow.Minutes();
         return db.AppActiveUsers<MembershipProvider>().Count(u => now - u.LastActivityDate > window);
     }
 }
 public override void UpdateUser(Sys.MembershipUser muser)
 {
     using (var db = new Context()) {
         User user = null;
         if (muser.ProviderUserKey is Guid) {
             var uid = (Guid)muser.ProviderUserKey;
             user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserId == uid);
         }
         if (user == null) user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == muser.UserName);
         if (user != null) {
             user.Comment = muser.Comment;
             user.CreationDate = muser.CreationDate;
             user.Email = muser.Email;
             user.IsApproved = muser.IsApproved;
             user.LastActivityDate = muser.LastLoginDate;
             user.LastLoginDate = muser.LastLoginDate;
             db.SaveChanges();
         }
     }
 }
 public override int GetPasswordFailuresSinceLastSuccess(string userName)
 {
     if (string.IsNullOrEmpty(userName)) {
         throw CreateArgumentNullOrEmptyException("userName");
     }
     using (var db = new Context()) {
         var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == userName);
         if (user == null) {
             throw new InvalidOperationException(string.Format("User not found: {0}", userName));
         }
         return user.PasswordFailuresSinceLastSuccess;
     }
 }
        public override bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            if (string.IsNullOrEmpty(userName)) {
                throw CreateArgumentNullOrEmptyException("userName");
            }
            if (string.IsNullOrEmpty(oldPassword)) {
                throw CreateArgumentNullOrEmptyException("oldPassword");
            }
            if (string.IsNullOrEmpty(newPassword)) {
                throw CreateArgumentNullOrEmptyException("newPassword");
            }
            if (!ValidPassword(newPassword)) return false;

            using (var db = new Context()) {
                try {
                    var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == userName);
                    if (user == null) {
                        return false;
                    }

                    bool validPassword = user.CheckPassword(oldPassword);
                    if (validPassword) user.SetPassword(this, newPassword);

                    // user.LastActivityDate = DateTime.UtcNow;

                    db.SaveChanges();
                    return validPassword;
                } catch {
                    return false;
                }
            }
        }
        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            using (var db = new Context()) {
                try {
                    var user = db.AppActiveUsers<MembershipProvider>().FirstOrDefault(u => u.UserName == username);
                    if (user == null) {
                        throw new InvalidOperationException(string.Format("User not found: {0}", username));
                    }
                    var validPassword = user.CheckPassword(password);
                    if (validPassword) {
                        user.PasswordQuestion = newPasswordQuestion;
                        user.PasswordAnswer = newPasswordAnswer;
                    }

                    db.SaveChanges();
                    return validPassword;
                } catch {
                    return false;
                }
            }
        }