public User CreateUser(string email, string password) { if (email == null) { throw new ArgumentNullException(nameof(email)); } if (password == null) { throw new ArgumentNullException(nameof(password)); } if (password.Length < 6) { throw new BusinessException("Password must be atleast 6 characters in length."); } var passwordComponent = new PasswordComponent(); var salt = passwordComponent.GetCryptographicSalt(); var user = new User { UserName = email, Password = passwordComponent.Hash(password, salt), PasswordSalt = salt, Email = email }; ValidateObject(user); context.Add(user); context.Commit(); return(user); }
public AuthorizationToken SignIn(AuthenticationToken token) { User user; try { user = context.Users.Single(p => p.UserName.Equals(token.UserName, StringComparison.OrdinalIgnoreCase)); } catch (InvalidOperationException) { throw new InvalidCredentialException("Invalid username or password."); } var passwordComponent = new PasswordComponent(); var password = passwordComponent.Hash(token.Password, user.PasswordSalt); if (user.Password != password) { throw new InvalidCredentialException("Invalid username or password."); } var authToken = new AuthorizationToken { UserName = user.UserName }; var userToken = new UserToken { UserId = user.UserId, ExpirationDate = DateTime.Now.AddDays(3), Token = authToken.Token }; context.Add(userToken); context.Commit(); return(authToken); }