protected virtual void SetAttempt(global::SoftFluent.Samples.GED.Security.User user, bool reset) { if (reset) { user.IsLockedOut = false; user.LastLockoutDate = DateTime.MinValue; user.FailedPasswordAttemptCount = 0; user.FailedPasswordAttemptWindowStart = DateTime.MinValue; } else { DateTime time = DateTime.UtcNow; time = new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second); if (time > user.FailedPasswordAttemptWindowStart.AddMinutes(PasswordAttemptWindow)) { user.FailedPasswordAttemptCount = 1; user.FailedPasswordAttemptWindowStart = time; } else { user.FailedPasswordAttemptCount++; } if (user.FailedPasswordAttemptCount >= MaxInvalidPasswordAttempts) { user.IsLockedOut = true; user.LastLockoutDate = time; } } }
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext sc, SettingsPropertyCollection properties) { SettingsPropertyValueCollection values = new SettingsPropertyValueCollection(); if (properties.Count == 0) { return(values); } string username = (string)sc["UserName"]; if (string.IsNullOrEmpty(username)) { return(values); } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); foreach (SettingsProperty property in properties) { SettingsPropertyValue value = new SettingsPropertyValue(property); if (user != null) { value.PropertyValue = ConvertUtilities.ChangeType(user.Properties[property.Name], property.PropertyType, property.DefaultValue); value.Deserialized = true; value.IsDirty = false; } values.Add(value); } return(values); }
public override int DeleteProfiles(string[] usernames) { if (usernames == null) { throw new ArgumentNullException("usernames"); } int count = 0; List <global::SoftFluent.Samples.GED.Security.User> users = new List <global::SoftFluent.Samples.GED.Security.User>(usernames.Length); foreach (string username in usernames) { if (username == null) { continue; } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { throw new ProviderException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("UserNotFound", "User '{0}' was not found.", new object[] { username })); } if (user.Delete()) { count++; } } return(count); }
public override string GetPassword(string username, string passwordAnswer) { if (!EnablePasswordRetrieval) { throw new NotSupportedException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("PasswordRetrievalUnsupported", "Membership provider does not support password retrieval.")); } if (username == null) { throw new ArgumentNullException("username"); } if (RequiresQuestionAndAnswer) { if (passwordAnswer == null) { throw new ArgumentNullException("passwordAnswer"); } } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { throw new ProviderException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("UserNotFound", "User '" + username + "' was not found.")); } return(user.Password); }
protected virtual bool CheckPassword(global::SoftFluent.Samples.GED.Security.User user, string password) { string encodedPassword = EncodePassword(password, user.PasswordSalt); return(encodedPassword == user.Password); }
public override System.Web.Security.MembershipUser GetUser(object providerUserKey, bool userIsOnline) { if (providerUserKey == null) { throw new ArgumentNullException("providerUserKey"); } if (!(providerUserKey is System.Guid)) { throw new ArgumentException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("InvalidProviderUserKeyType", "Invalid providerUserKey type"), "providerUserKey"); } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.Load((System.Guid)providerUserKey); if (user == null) { return(null); } if (userIsOnline) { UpdateActivity(user, false); } return(MembershipUserFromUser(user)); }
public override void AddUsersToRoles(string[] usernames, string[] roleNames) { if (usernames == null) { throw new ArgumentNullException("usernames"); } if (roleNames == null) { throw new ArgumentNullException("roleNames"); } List <global::SoftFluent.Samples.GED.Security.User> users = new List <global::SoftFluent.Samples.GED.Security.User>(usernames.Length); foreach (string username in usernames) { if (username == null) { continue; } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { throw new ProviderException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("UserNotFound", "User '{0}' was not found.", new object[] { username })); } users.Add(user); } foreach (string roleName in roleNames) { if (roleName == null) { continue; } global::SoftFluent.Samples.GED.Security.Role role = global::SoftFluent.Samples.GED.Security.Role.LoadByName(roleName); if (role == null) { throw new ProviderException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("RoleNotFound", "Role '{0}' was not found.", new object[] { roleName })); } foreach (global::SoftFluent.Samples.GED.Security.User user in users) { global::SoftFluent.Samples.GED.Security.UserRole userRole = new global::SoftFluent.Samples.GED.Security.UserRole(); userRole.User = user; userRole.Role = role; try { userRole.Save(); } catch (CodeFluent.Runtime.CodeFluentDuplicateException) { // do nothing } } } }
private static string GetName(global::SoftFluent.Samples.GED.Security.User user) { if (user == null) { throw new ArgumentNullException("user"); } return(user.UserName); }
protected override bool OnAuthenticate(System.Web.HttpContext context, string login, string password, string realm) { global::SoftFluent.Samples.GED.Security.User user = MembershipProvider.Current.ValidateAndGetUser(login, password, true); if (user == null) { return(false); } context.User = new Principal(new Identity(user)); return(true); }
public override void SetPropertyValues(SettingsContext sc, SettingsPropertyValueCollection properties) { string username = (string)sc["UserName"]; if ((string.IsNullOrEmpty(username)) || (properties.Count == 0)) { return; } bool isAnonymous = !(bool)sc["IsAuthenticated"]; bool dirty = false; foreach (SettingsPropertyValue value in properties) { if ((value.IsDirty) && ((!isAnonymous) || ((bool)value.Property.Attributes["AllowAnonymous"]))) { dirty = true; break; } } if (!dirty) { return; } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { user = new global::SoftFluent.Samples.GED.Security.User(); user.UserName = username; user.Email = string.Format(EmailFormat, username); } user.IsAnonymous = isAnonymous; if (user.Properties == null) { user.Properties = new System.Collections.Specialized.OrderedDictionary(System.StringComparer.CurrentCultureIgnoreCase); } foreach (SettingsPropertyValue value in properties) { if (((value.IsDirty) || (!(value.UsingDefaultValue))) && ((!isAnonymous) || ((bool)value.Property.Attributes["AllowAnonymous"]))) { user.Properties[value.Name] = value.PropertyValue; } } SoftFluent.Samples.GED.Web.Security.MembershipProvider.UpdateUserActivity(user, false); user.Save(); }
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { if (usernames == null) { throw new ArgumentNullException("usernames"); } if (roleNames == null) { throw new ArgumentNullException("roleNames"); } List <global::SoftFluent.Samples.GED.Security.User> users = new List <global::SoftFluent.Samples.GED.Security.User>(usernames.Length); foreach (string username in usernames) { if (username == null) { continue; } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { throw new ProviderException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("UserNotFound", "User '{0}' was not found.", new object[] { username })); } users.Add(user); } foreach (string roleName in roleNames) { if (roleName == null) { continue; } global::SoftFluent.Samples.GED.Security.Role role = global::SoftFluent.Samples.GED.Security.Role.LoadByName(roleName); if (role == null) { throw new ProviderException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("RoleNotFound", "Role '{0}' was not found.", new object[] { roleName })); } foreach (global::SoftFluent.Samples.GED.Security.User user in users) { global::SoftFluent.Samples.GED.Security.UserRole userRole = global::SoftFluent.Samples.GED.Security.UserRole.LoadByUserUserNameAndRoleName(user.UserName, role.Name); if (userRole != null) { userRole.Delete(); } } } }
public virtual ProfileInfo ProfileInfoFromUser(global::SoftFluent.Samples.GED.Security.User user) { if (user == null) { return(null); } return(new ProfileInfo( user.UserName, user.IsAnonymous, user.LastActivityDate.ToLocalTime(), DateTime.MinValue, 0)); }
public static void UpdateUserActivity(global::SoftFluent.Samples.GED.Security.User user, bool login) { if (user == null) { throw new ArgumentNullException("user"); } DateTime time = DateTime.UtcNow; time = new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second); if (login) { user.LastLoginDate = time; } user.LastActivityDate = time; }
public override string GetUserNameByEmail(string email) { if (string.IsNullOrEmpty(email)) { return(null); } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByEmail(email); if (user == null) { return(null); } return(user.UserName); }
public override bool DeleteUser(string username, bool deleteAllRelatedData) { if (username == null) { throw new ArgumentNullException("username"); } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { return(false); } return(user.Delete()); }
public override bool UnlockUser(string username) { global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { return(false); } SetAttempt(user, true); user.Save(); return(true); }
public virtual void UpdateActivity(global::SoftFluent.Samples.GED.Security.User user, bool login) { if (user == null) { throw new ArgumentNullException("user"); } UpdateUserActivity(user, login); if (UpdateLastActivity) { global::SoftFluent.Samples.GED.Security.User.UpdateActivity(user.Id, user.LastActivityDate, user.LastLoginDate); } }
public override void UpdateUser(System.Web.Security.MembershipUser member) { global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(member.UserName); if (user == null) { return; } user.Email = member.Email; user.LastLoginDate = member.LastLoginDate.ToUniversalTime(); user.LastActivityDate = member.LastActivityDate.ToUniversalTime(); user.Save(); }
public override string[] GetRolesForUser(string username) { if (string.IsNullOrEmpty(username)) { throw new ArgumentNullException("username"); } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { throw new ProviderException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("UserNotFound", "User was not found.")); } return(GetRoleNames(user.Roles)); }
public override string ResetPassword(string username, string passwordAnswer) { if (!EnablePasswordReset) { throw new NotSupportedException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("PasswordResetUnsupported", "Membership provider is not configured to support password resets.")); } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { throw new ProviderException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("UserNotFound", "User '" + username + "' was not found.")); } if (user.IsLockedOut) { throw new MembershipPasswordException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("UserLockedOut", "User '" + username + "' is locked out.")); } string password = Membership.GeneratePassword(MinRequiredPasswordLength, MinRequiredNonAlphanumericCharacters); user.Password = EncodePassword(password, user.PasswordSalt); ValidatePasswordEventArgs e = new ValidatePasswordEventArgs(username, user.Password, false); OnValidatingPassword(e); if (e.Cancel) { if (e.FailureInformation != null) { throw e.FailureInformation; } throw new ProviderException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("PasswordInvalidCustom", "Password is invalid")); } user.Save(); return(password); }
public virtual global::SoftFluent.Samples.GED.Security.User ValidateAndGetUser(string username, string password, bool login) { if (username == null) { throw new ArgumentNullException("username"); } if (password == null) { throw new ArgumentNullException("password"); } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { return(null); } if (user.IsLockedOut) { return(null); } string encodedPassword = EncodePassword(password, user.PasswordSalt); if (encodedPassword != user.Password) { SetAttempt(user, false); user.Save(); return(null); } UpdateActivity(user, login); return(user); }
public virtual MembershipUser MembershipUserFromUser(global::SoftFluent.Samples.GED.Security.User user) { if (user == null) { return(null); } return(new MembershipUser(Name, user.UserName, user.Id, user.Email, null, null, true, user.IsLockedOut, DateTime.MinValue, user.LastLoginDate, user.LastActivityDate, user.LastPasswordChangeDate, user.LastLockoutDate )); }
public override System.Web.Security.MembershipUser GetUser(string username, bool userIsOnline) { if (username == null) { throw new ArgumentNullException("username"); } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { return(null); } if (userIsOnline) { UpdateActivity(user, false); } return(MembershipUserFromUser(user)); }
public override bool ChangePassword(string username, string oldPassword, string newPassword) { if ((string.IsNullOrEmpty(username)) || (username.Length > 256)) { throw new ArgumentNullException("username"); } if ((string.IsNullOrEmpty(oldPassword)) || (oldPassword.Length > 256)) { throw new ArgumentNullException("oldPassword"); } if ((string.IsNullOrEmpty(newPassword)) || (newPassword.Length > 256)) { throw new ArgumentNullException("newPassword"); } if (newPassword.Length < MinRequiredPasswordLength) { throw new ArgumentException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("PasswordTooShort", "Password is too short. Minimum length is {0}", new object[] { MinRequiredPasswordLength })); } int min = 0; for (int i = 0; i < newPassword.Length; i++) { if (!char.IsLetterOrDigit(newPassword, i)) { min++; } } if (min < MinRequiredNonAlphanumericCharacters) { throw new ArgumentException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("PasswordTooAlpha", "Password needs more non alpha numeric characters. Minimum is {0}", new object[] { MinRequiredNonAlphanumericCharacters })); } if ((PasswordStrengthRegularExpression.Length > 0) && !Regex.IsMatch(newPassword, PasswordStrengthRegularExpression)) { throw new ArgumentException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("PasswordInvalid", "Password does not match regular expression")); } global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { return(false); } if (!CheckPassword(user, oldPassword)) { SetAttempt(user, false); user.Save(); return(false); } string encodedPassword = EncodePassword(newPassword, user.PasswordSalt); if (encodedPassword.Length > 256) { SetAttempt(user, false); user.Save(); throw new ArgumentException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("PasswordTooLong", "Password is too long")); } ValidatePasswordEventArgs e = new ValidatePasswordEventArgs(username, encodedPassword, false); OnValidatingPassword(e); if (e.Cancel) { if (e.FailureInformation != null) { throw e.FailureInformation; } throw new ArgumentException(SoftFluent.Samples.GED.Resources.Manager.GetStringWithDefault("PasswordInvalidCustom", "Password is invalid")); } user.Password = encodedPassword; user.Save(); return(true); }
public override void SetPropertyValues(SettingsContext sc, SettingsPropertyValueCollection properties) { string username = (string)sc["UserName"]; if ((string.IsNullOrEmpty(username)) || (properties.Count == 0)) return; bool isAnonymous = !(bool)sc["IsAuthenticated"]; bool dirty = false; foreach(SettingsPropertyValue value in properties) { if ((value.IsDirty) && ((!isAnonymous) || ((bool)value.Property.Attributes["AllowAnonymous"]))) { dirty = true; break; } } if (!dirty) return; global::SoftFluent.Samples.GED.Security.User user = global::SoftFluent.Samples.GED.Security.User.LoadByUserName(username); if (user == null) { user = new global::SoftFluent.Samples.GED.Security.User(); user.UserName = username; user.Email = string.Format(EmailFormat, username); } user.IsAnonymous = isAnonymous; if (user.Properties == null) { user.Properties = new System.Collections.Specialized.OrderedDictionary(System.StringComparer.CurrentCultureIgnoreCase); } foreach(SettingsPropertyValue value in properties) { if (((value.IsDirty) || (!(value.UsingDefaultValue))) && ((!isAnonymous) || ((bool)value.Property.Attributes["AllowAnonymous"]))) { user.Properties[value.Name] = value.PropertyValue; } } SoftFluent.Samples.GED.Web.Security.MembershipProvider.UpdateUserActivity(user, false); user.Save(); }
protected virtual void SetAnswerAttempt(global::SoftFluent.Samples.GED.Security.User user, bool reset) { }
public Identity(global::SoftFluent.Samples.GED.Security.User user) :base(GetName(user), "SoftFluent.Samples.GED") { _user = user; }
public Identity(global::SoftFluent.Samples.GED.Security.User user) : base(GetName(user), "SoftFluent.Samples.GED") { _user = user; }
public override System.Web.Security.MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { global::SoftFluent.Samples.GED.Security.User user = new global::SoftFluent.Samples.GED.Security.User(); if (providerUserKey != null) { if (providerUserKey is System.Guid) { user.Id = (System.Guid)providerUserKey; } else { status = MembershipCreateStatus.InvalidProviderUserKey; return(null); } } email = ConvertUtilities.Nullify(email, true); if ((email != null) && (email.Length > 256)) { status = MembershipCreateStatus.InvalidEmail; return(null); } if ((email == null) && RequiresUniqueEmail) { status = MembershipCreateStatus.InvalidEmail; return(null); } user.Email = email; password = ConvertUtilities.Nullify(password, true); user.PasswordSalt = GenerateSalt(); string encodedPassword = EncodePassword(password, user.PasswordSalt); if ((password == null) || (password.Length > 256)) { status = MembershipCreateStatus.InvalidPassword; return(null); } user.Password = encodedPassword; username = ConvertUtilities.Nullify(username, true); if ((username == null) || (username.Length > 256)) { status = MembershipCreateStatus.InvalidUserName; return(null); } user.UserName = username; DateTime time = DateTime.UtcNow; time = new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second); user.IsLockedOut = false; user.LastLoginDate = time; user.LastActivityDate = time; user.LastPasswordChangeDate = time; user.LastLockoutDate = DateTime.MinValue; if (password.Length < MinRequiredPasswordLength) { status = MembershipCreateStatus.InvalidPassword; return(null); } int min = 0; for (int i = 0; i < password.Length; i++) { if (!char.IsLetterOrDigit(password, i)) { min++; } } if (min < MinRequiredNonAlphanumericCharacters) { status = MembershipCreateStatus.InvalidPassword; return(null); } if ((!string.IsNullOrEmpty(PasswordStrengthRegularExpression)) && (!Regex.IsMatch(password, PasswordStrengthRegularExpression))) { status = MembershipCreateStatus.InvalidPassword; return(null); } ValidatePasswordEventArgs e = new ValidatePasswordEventArgs(username, password, true); OnValidatingPassword(e); if (e.Cancel) { status = MembershipCreateStatus.InvalidPassword; return(null); } try { user.Save(); } catch (CodeFluent.Runtime.CodeFluentDuplicateException) { status = MembershipCreateStatus.DuplicateProviderUserKey; return(null); } status = MembershipCreateStatus.Success; return(MembershipUserFromUser(user)); }
public override System.Web.Security.MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { global::SoftFluent.Samples.GED.Security.User user = new global::SoftFluent.Samples.GED.Security.User(); if (providerUserKey != null) { if (providerUserKey is System.Guid) { user.Id = (System.Guid)providerUserKey; } else { status = MembershipCreateStatus.InvalidProviderUserKey; return null; } } email = ConvertUtilities.Nullify(email, true); if ((email != null) && (email.Length > 256)) { status = MembershipCreateStatus.InvalidEmail; return null; } if ((email == null) && RequiresUniqueEmail) { status = MembershipCreateStatus.InvalidEmail; return null; } user.Email = email; password = ConvertUtilities.Nullify(password, true); user.PasswordSalt = GenerateSalt(); string encodedPassword = EncodePassword(password, user.PasswordSalt); if ((password == null) || (password.Length > 256)) { status = MembershipCreateStatus.InvalidPassword; return null; } user.Password = encodedPassword; username = ConvertUtilities.Nullify(username, true); if ((username == null) || (username.Length > 256)) { status = MembershipCreateStatus.InvalidUserName; return null; } user.UserName = username; DateTime time = DateTime.UtcNow; time = new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second); user.IsLockedOut = false; user.LastLoginDate = time; user.LastActivityDate = time; user.LastPasswordChangeDate = time; user.LastLockoutDate = DateTime.MinValue; if (password.Length < MinRequiredPasswordLength) { status = MembershipCreateStatus.InvalidPassword; return null; } int min = 0; for (int i = 0; i < password.Length; i++) { if (!char.IsLetterOrDigit(password, i)) { min++; } } if (min < MinRequiredNonAlphanumericCharacters) { status = MembershipCreateStatus.InvalidPassword; return null; } if ((!string.IsNullOrEmpty(PasswordStrengthRegularExpression)) && (!Regex.IsMatch(password, PasswordStrengthRegularExpression))) { status = MembershipCreateStatus.InvalidPassword; return null; } ValidatePasswordEventArgs e = new ValidatePasswordEventArgs(username, password, true); OnValidatingPassword(e); if (e.Cancel) { status = MembershipCreateStatus.InvalidPassword; return null; } try { user.Save(); } catch(CodeFluent.Runtime.CodeFluentDuplicateException) { status = MembershipCreateStatus.DuplicateProviderUserKey; return null; } status = MembershipCreateStatus.Success; return MembershipUserFromUser(user); }