/// <summary> /// Return the user with the specified Id /// </summary> /// <param name="userName"></param> /// <returns></returns> public FlightLogUser GetUser(int userId) { FlightLogUser user = _context.FlightLogUsers.FirstOrDefault(u => u.Id == userId); ThrowIfUserNotFound(user, userId); return(user); }
public async Task GetUserByIdAsyncTest() { FlightLogUser user = await _factory.Users.GetUserAsync(_userId); Assert.AreEqual(UserName, user.UserName); Assert.AreNotEqual(Password, user.Password); }
public void GetUserByIdTest() { FlightLogUser user = _factory.Users.GetUser(_userId); Assert.AreEqual(UserName, user.UserName); Assert.AreNotEqual(Password, user.Password); }
/// <summary> /// Return the user with the specified Id /// </summary> /// <param name="userName"></param> /// <returns></returns> public FlightLogUser GetUser(string userName) { FlightLogUser user = _context.FlightLogUsers.FirstOrDefault(u => u.UserName == userName); ThrowIfUserNotFound(user, userName); return(user); }
/// <summary> /// Return the user with the specified Id /// </summary> /// <param name="userName"></param> /// <returns></returns> public async Task <FlightLogUser> GetUserAsync(string userName) { FlightLogUser user = await _context.FlightLogUsers.FirstOrDefaultAsync(u => u.UserName == userName); ThrowIfUserNotFound(user, userName); return(user); }
/// <summary> /// Delete the specified user /// </summary> /// <param name="userName"></param> public async Task DeleteUserAsync(string userName) { FlightLogUser user = await GetUserAsync(userName); _context.FlightLogUsers.Remove(user); await _context.SaveChangesAsync(); }
/// <summary> /// Set the password for the specified user /// </summary> /// <param name="userName"></param> /// <param name="password"></param> public void SetPassword(string userName, string password) { FlightLogUser user = GetUser(userName); user.Password = _hasher.Value.HashPassword(userName, password); _context.SaveChanges(); }
private void ThrowIfUserFound(FlightLogUser user, object userId) { if (user != null) { throw new UserExistsException($"User {userId} already exists"); } }
/// <summary> /// Delete the specified user /// </summary> /// <param name="userName"></param> public void DeleteUser(string userName) { FlightLogUser user = GetUser(userName); _context.FlightLogUsers.Remove(user); _context.SaveChanges(); }
/// <summary> /// Set the password for the specified user /// </summary> /// <param name="userName"></param> /// <param name="password"></param> public async Task SetPasswordAsync(string userName, string password) { FlightLogUser user = await GetUserAsync(userName); user.Password = _hasher.Value.HashPassword(userName, password); await _context.SaveChangesAsync(); }
/// <summary> /// Return the user with the specified Id /// </summary> /// <param name="userName"></param> /// <returns></returns> public async Task <FlightLogUser> GetUserAsync(int userId) { FlightLogUser user = await _context.FlightLogUsers.FirstOrDefaultAsync(u => u.Id == userId); ThrowIfUserNotFound(user, userId); return(user); }
private void ThrowIfUserNotFound(FlightLogUser user, object userId) { if (user == null) { string message = $"User {userId} not found"; throw new UserNotFoundException(message); } }
public async Task AddUserAsyncTest() { FlightLogUser user = await _factory.Users.AddUserAsync(AsyncUserName, Password); await _factory.Context.SaveChangesAsync(); Assert.AreEqual(2, _factory.Context.FlightLogUsers.Count()); Assert.AreEqual(AsyncUserName, user.UserName); Assert.AreNotEqual(Password, user.Password); }
public void TestInitialize() { DroneFlightLogDbContext context = new DroneFlightLogDbContextFactory().CreateDbContext(null); _factory = new DroneFlightLogFactory <DroneFlightLogDbContext>(context); FlightLogUser user = _factory.Users.AddUser(UserName, Password); _factory.Context.SaveChanges(); _userId = user.Id; }
/// <summary> /// Authenticate the specified user /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public bool Authenticate(string userName, string password) { FlightLogUser user = GetUser(userName); PasswordVerificationResult result = _hasher.Value.VerifyHashedPassword(userName, user.Password, password); if (result == PasswordVerificationResult.SuccessRehashNeeded) { user.Password = _hasher.Value.HashPassword(userName, password); _context.SaveChanges(); } return(result != PasswordVerificationResult.Failed); }
/// <summary> /// Authenticate the specified user /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public async Task <bool> AuthenticateAsync(string userName, string password) { FlightLogUser user = await GetUserAsync(userName); PasswordVerificationResult result = _hasher.Value.VerifyHashedPassword(userName, user.Password, password); if (result == PasswordVerificationResult.SuccessRehashNeeded) { user.Password = _hasher.Value.HashPassword(userName, password); await _context.SaveChangesAsync(); } return(result != PasswordVerificationResult.Failed); }
/// <summary> /// Add a new user, given their details /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public FlightLogUser AddUser(string userName, string password) { FlightLogUser user = _context.FlightLogUsers.FirstOrDefault(u => u.UserName == userName); ThrowIfUserFound(user, userName); user = new FlightLogUser { UserName = userName, Password = _hasher.Value.HashPassword(userName, password) }; _context.FlightLogUsers.Add(user); _context.SaveChanges(); return(user); }
/// <summary> /// Add a new user, given their details /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public async Task <FlightLogUser> AddUserAsync(string userName, string password) { FlightLogUser user = await _context.FlightLogUsers.FirstOrDefaultAsync(u => u.UserName == userName); ThrowIfUserFound(user, userName); user = new FlightLogUser { UserName = userName, Password = _hasher.Value.HashPassword(userName, password) }; await _context.FlightLogUsers.AddAsync(user); await _context.SaveChangesAsync(); return(user); }
/// <summary> /// Authenticate the specified user and, if successful, return the serialized JWT token /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public async Task <string> AuthenticateAsync(string userName, string password) { string serializedToken = null; bool authenticated = await _factory.Users.AuthenticateAsync(userName, password); if (authenticated) { // The user ID is used to construct the claim FlightLogUser user = await _factory.Users.GetUserAsync(userName); // Construct the information needed to populate the token descriptor byte[] key = Encoding.ASCII.GetBytes(_settings.Secret); SigningCredentials credentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature); DateTime expiry = DateTime.UtcNow.AddMinutes(_settings.TokenLifespanMinutes); // Create the descriptor containing the information used to create the JWT token SecurityTokenDescriptor descriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.UserName) }), Expires = expiry, SigningCredentials = credentials }; // Use the descriptor to create the JWT token then serialize it to // a string JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler(); SecurityToken token = handler.CreateToken(descriptor); serializedToken = handler.WriteToken(token); } return(serializedToken); }