public override bool DeleteUser(string username, bool deleteAllRelatedData) { SecUtility.CheckParameter(ref username, true, true, true, 0x100, "username"); var user = db.Users.Include("Roles").FirstOrDefault(u => u.UserName == username); if (user == null) { return(false); } foreach (var role in user.Roles) { user.Roles.Remove(role); } db.Users.Remove(user); db.SaveChanges(); return(true); }
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { SecUtility.CheckParameter(ref roleName, true, true, true, 256, "roleName"); var query = from r in db.Roles where r.RoleName == roleName select new { r, child = r.Users.Count() }; if (query.Any()) { var r = query.First(); if (r.child > 0 && throwOnPopulatedRole) { throw new ProviderException("Role_is_not_empty"); } db.Roles.Remove(r.r); db.SaveChanges(); return(true); } return(false); }
public override bool RoleExists(string roleName) { SecUtility.CheckParameter(ref roleName, true, true, true, 256, "roleName"); return(db.Roles.Where(r => r.RoleName == roleName).Any()); }
public override string ResetPassword(string username, string answer) { if (!EnablePasswordReset) { throw new NotSupportedException("Not_configured_to_support_password_resets"); } SecUtility.CheckParameter(ref username, true, true, true, 256, "username"); var user = GetDBUser(username); var passwordAnswer = answer; string encodedPasswordAnswer; if (passwordAnswer != null) { passwordAnswer = passwordAnswer.Trim(); } if (!string.IsNullOrEmpty(passwordAnswer)) { encodedPasswordAnswer = EncodePassword(passwordAnswer.ToLower(CultureInfo.InvariantCulture), user.PasswordFormat, user.PasswordSalt); } else { encodedPasswordAnswer = passwordAnswer; } SecUtility.CheckParameter(ref encodedPasswordAnswer, RequiresQuestionAndAnswer, RequiresQuestionAndAnswer, false, 128, "passwordAnswer"); string newPassword = GeneratePassword(); ValidatePasswordEventArgs e = new ValidatePasswordEventArgs(username, newPassword, false); OnValidatingPassword(e); if (e.Cancel) { if (e.FailureInformation != null) { throw e.FailureInformation; } else { throw new ProviderException("Membership_Custom_Password_Validation_Failure"); } } var utc = DateTime.UtcNow; if (!answer.Equals(user.PasswordAnswer, StringComparison.CurrentCultureIgnoreCase)) { if (utc > user.FailedPasswordAnswerAttemptWindowStart.AddMinutes(PasswordAttemptWindow)) { user.FailedPasswordAnswerAttemptCount = 1; } else { user.FailedPasswordAnswerAttemptCount++; } user.FailedPasswordAnswerAttemptWindowStart = utc; if (user.FailedPasswordAnswerAttemptCount > MaxInvalidPasswordAttempts) { user.LastLockoutDate = DateTime.UtcNow; user.Status = (byte)A1fxCrm.Web.Framework.Model.Enumerations.UserStatus.Locked; } db.SaveChanges(); return(null); } user.FailedPasswordAnswerAttemptCount = 0; user.FailedPasswordAnswerAttemptWindowStart = new DateTime(1754, 01, 01); user.FailedPasswordAttemptCount = 0; user.FailedPasswordAttemptWindowStart = user.FailedPasswordAnswerAttemptWindowStart; user.Password = EncodePassword(newPassword, user.PasswordFormat, user.PasswordSalt); db.SaveChanges(); return(newPassword); }