/// <summary>Updates the user authentication.</summary> /// /// <param name="existingUser">The existing user.</param> /// <param name="newUser"> The new user.</param> /// <param name="password"> The password.</param> /// /// <returns>An UserAuth.</returns> public UserAuth UpdateUserAuth(UserAuth existingUser, UserAuth newUser, string password) { ValidateNewUser(newUser, password); using (var redis = factory.GetClient()) { AssertNoExistingUser(redis, newUser, existingUser); if (existingUser.UserName != newUser.UserName && existingUser.UserName != null) { redis.RemoveEntryFromHash(IndexUserNameToUserId, existingUser.UserName); } if (existingUser.Email != newUser.Email && existingUser.Email != null) { redis.RemoveEntryFromHash(IndexEmailToUserId, existingUser.Email); } var hash = existingUser.PasswordHash; var salt = existingUser.Salt; if (password != null) { var saltedHash = new SaltedHash(); saltedHash.GetHashAndSaltString(password, out hash, out salt); } // If either one changes the digest hash has to be recalculated var digestHash = existingUser.DigestHa1Hash; if (password != null || existingUser.UserName != newUser.UserName) { var digestHelper = new DigestAuthFunctions(); digestHash = digestHelper.CreateHa1(newUser.UserName, DigestAuthProvider.Realm, password); } newUser.Id = existingUser.Id; newUser.PasswordHash = hash; newUser.Salt = salt; newUser.CreatedDate = existingUser.CreatedDate; newUser.ModifiedDate = DateTime.UtcNow; var userId = newUser.Id.ToString(CultureInfo.InvariantCulture); if (!newUser.UserName.IsNullOrEmpty()) { redis.SetEntryInHash(IndexUserNameToUserId, newUser.UserName, userId); } if (!newUser.Email.IsNullOrEmpty()) { redis.SetEntryInHash(IndexEmailToUserId, newUser.Email, userId); } redis.Store(newUser); return(newUser); } }
/// <summary>Attempts to authenticate from the given data.</summary> /// /// <param name="userName">Name of the user.</param> /// <param name="password">The password.</param> /// <param name="userAuth">The user authentication.</param> /// /// <returns>true if it succeeds, false if it fails.</returns> public virtual bool TryAuthenticate(string userName, string password, out UserAuth userAuth) { //userId = null; userAuth = GetUserAuthByUserName(userName); if (userAuth == null) { return(false); } var saltedHash = new SaltedHash(); if (saltedHash.VerifyHashString(password, userAuth.PasswordHash, userAuth.Salt)) { return(true); } userAuth = null; return(false); }
/// <summary>Creates user authentication.</summary> /// /// <param name="newUser"> The new user.</param> /// <param name="password">The password.</param> /// /// <returns>The new user authentication.</returns> public virtual UserAuth CreateUserAuth(UserAuth newUser, string password) { ValidateNewUser(newUser, password); using (var redis = factory.GetClient()) { AssertNoExistingUser(redis, newUser); var saltedHash = new SaltedHash(); string salt; string hash; saltedHash.GetHashAndSaltString(password, out hash, out salt); newUser.Id = redis.As <UserAuth>().GetNextSequence(); newUser.PasswordHash = hash; newUser.Salt = salt; var digestHelper = new DigestAuthFunctions(); newUser.DigestHa1Hash = digestHelper.CreateHa1(newUser.UserName, DigestAuthProvider.Realm, password); newUser.CreatedDate = DateTime.UtcNow; newUser.ModifiedDate = newUser.CreatedDate; var userId = newUser.Id.ToString(CultureInfo.InvariantCulture); if (!newUser.UserName.IsNullOrEmpty()) { redis.SetEntryInHash(IndexUserNameToUserId, newUser.UserName, userId); } if (!newUser.Email.IsNullOrEmpty()) { redis.SetEntryInHash(IndexEmailToUserId, newUser.Email, userId); } redis.Store(newUser); return(newUser); } }
/// <summary>Attempts to authenticate from the given data.</summary> /// /// <param name="userName">Name of the user.</param> /// <param name="password">The password.</param> /// <param name="userAuth">The user authentication.</param> /// /// <returns>true if it succeeds, false if it fails.</returns> public virtual bool TryAuthenticate(string userName, string password, out UserAuth userAuth) { //userId = null; userAuth = GetUserAuthByUserName(userName); if (userAuth == null) return false; var saltedHash = new SaltedHash(); if (saltedHash.VerifyHashString(password, userAuth.PasswordHash, userAuth.Salt)) { return true; } userAuth = null; return false; }
/// <summary>Updates the user authentication.</summary> /// /// <param name="existingUser">The existing user.</param> /// <param name="newUser"> The new user.</param> /// <param name="password"> The password.</param> /// /// <returns>An UserAuth.</returns> public UserAuth UpdateUserAuth(UserAuth existingUser, UserAuth newUser, string password) { ValidateNewUser(newUser, password); using (var redis = factory.GetClient()) { AssertNoExistingUser(redis, newUser, existingUser); if (existingUser.UserName != newUser.UserName && existingUser.UserName != null) { redis.RemoveEntryFromHash(IndexUserNameToUserId, existingUser.UserName); } if (existingUser.Email != newUser.Email && existingUser.Email != null) { redis.RemoveEntryFromHash(IndexEmailToUserId, existingUser.Email); } var hash = existingUser.PasswordHash; var salt = existingUser.Salt; if (password != null) { var saltedHash = new SaltedHash(); saltedHash.GetHashAndSaltString(password, out hash, out salt); } // If either one changes the digest hash has to be recalculated var digestHash = existingUser.DigestHa1Hash; if (password != null || existingUser.UserName != newUser.UserName) { var digestHelper = new DigestAuthFunctions(); digestHash = digestHelper.CreateHa1(newUser.UserName, DigestAuthProvider.Realm, password); } newUser.Id = existingUser.Id; newUser.PasswordHash = hash; newUser.Salt = salt; newUser.CreatedDate = existingUser.CreatedDate; newUser.ModifiedDate = DateTime.UtcNow; var userId = newUser.Id.ToString(CultureInfo.InvariantCulture); if (!newUser.UserName.IsNullOrEmpty()) redis.SetEntryInHash(IndexUserNameToUserId, newUser.UserName, userId); if (!newUser.Email.IsNullOrEmpty()) redis.SetEntryInHash(IndexEmailToUserId, newUser.Email, userId); redis.Store(newUser); return newUser; } }
/// <summary>Creates user authentication.</summary> /// /// <param name="newUser"> The new user.</param> /// <param name="password">The password.</param> /// /// <returns>The new user authentication.</returns> public virtual UserAuth CreateUserAuth(UserAuth newUser, string password) { ValidateNewUser(newUser, password); using (var redis = factory.GetClient()) { AssertNoExistingUser(redis, newUser); var saltedHash = new SaltedHash(); string salt; string hash; saltedHash.GetHashAndSaltString(password, out hash, out salt); newUser.Id = redis.As<UserAuth>().GetNextSequence(); newUser.PasswordHash = hash; newUser.Salt = salt; var digestHelper = new DigestAuthFunctions(); newUser.DigestHa1Hash = digestHelper.CreateHa1(newUser.UserName, DigestAuthProvider.Realm, password); newUser.CreatedDate = DateTime.UtcNow; newUser.ModifiedDate = newUser.CreatedDate; var userId = newUser.Id.ToString(CultureInfo.InvariantCulture); if (!newUser.UserName.IsNullOrEmpty()) redis.SetEntryInHash(IndexUserNameToUserId, newUser.UserName, userId); if (!newUser.Email.IsNullOrEmpty()) redis.SetEntryInHash(IndexEmailToUserId, newUser.Email, userId); redis.Store(newUser); return newUser; } }