/// <summary> /// This method will encrypt and unencrypted password with the current password settings for dashboard. /// </summary> /// <param name="unencryptedPassword"></param> /// <param name="salt"></param> /// <returns></returns> public static string EncryptPassword(string unencryptedPassword, string salt) { MembershipProvider provider = new SecurityUtility().GetMembershipProvider(); string password; switch (provider.PasswordFormat) { case MembershipPasswordFormat.Clear: password = unencryptedPassword; break; case MembershipPasswordFormat.Encrypted: password = EncryptData(string.Format("{0}{1}", unencryptedPassword, salt)); break; case MembershipPasswordFormat.Hashed: password = CreateSHAHash(string.Format("{0}{1}", unencryptedPassword, salt)); break; default: password = unencryptedPassword; break; } return(password); }
public void Register(RegisterCommand command) { ISecurityUtility securityUtility = new SecurityUtility(); ThrowError.Against<ArgumentException>(string.IsNullOrEmpty(command.UserName), String.Format(ErrorMessage.IsRequired, "Tên đăng nhập")); ThrowError.Against<ArgumentException>(string.IsNullOrEmpty(command.Password), String.Format(ErrorMessage.IsRequired, "Mật khẩu")); var user = securityUtility.GetUserByUsername(command.UserName); ThrowError.Against<ArgumentException>(user != null, String.Format(ErrorMessage.Exists, "Tên đăng nhập")); ThrowError.Against<ArgumentException>(_personService.Query(t=>t.Email == command.Email).Select().Any(), String.Format(ErrorMessage.Exists, "Email")); // ThrowError.Against<ArgumentException>(!securityUtility.IsPasswordValid(command.Password), String.Format(ErrorMessage.IsPassword)); var person = new Person() { Email = command.Email, FullName = command.FullName, PersonId = Guid.NewGuid() }; user = new User() { Type = command.Type, UserName = command.UserName, CreationDate = DateTime.Now, Locked = false, PersonId = person.PersonId, Password = Cryptography.EncryptPassword(command.Password, "") }; _unitOfWork.Repository<Domain.Entity.User>().Insert(user); _personService.Insert(person); _unitOfWork.SaveChanges(); }
/// <summary> /// This method will encrypt and unencrypted password with the current password settings for dashboard. /// </summary> /// <param name="unencryptedPassword"></param> /// <param name="salt"></param> /// <returns></returns> public static string EncryptPassword(string unencryptedPassword, string salt) { MembershipProvider provider = new SecurityUtility().GetMembershipProvider(); string password; switch (provider.PasswordFormat) { case MembershipPasswordFormat.Clear: password = unencryptedPassword; break; case MembershipPasswordFormat.Encrypted: password = EncryptData(string.Format("{0}{1}", unencryptedPassword, salt)); break; case MembershipPasswordFormat.Hashed: password = CreateSHAHash(string.Format("{0}{1}", unencryptedPassword, salt)); break; default: password = unencryptedPassword; break; } return password; }
internal virtual void Init(string username, string password) { ISecurityUtility securityUtility = new SecurityUtility(); var membershipProvider = securityUtility.GetMembershipProvider(); if (!membershipProvider.ValidateUser(username, password)) throw new ApplicationException(string.Format("Failed to init Session Object. Invalid username or password. Username: {0}. Password {1}", username, password)); var user = SecurityUtility.GetUserByUsername(username); if (user == null) throw new ApplicationException(string.Format("Cannot retrieve user for username {0}.", username)); Init(user); }