public async Task <AuthenticationResponse> Handle(Query query, CancellationToken cancellationToken) { var user = await _appDatabaseContext.Set <User>() .SingleOrDefaultAsync(x => x.Username.ToLower() == query.Username.ToLower(), cancellationToken); if (user == null) { throw new Exception($"Could not find user: { query.Username }"); } var hashedPassword = _passwordHasher.HashPassword(user.Salt, query.Password); if (!ValidUser(user, hashedPassword)) { throw new Exception($"Could not validate user: { query.Username }"); } var schemaType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"; var claims = user?.Roles.Select(x => new Claim(schemaType, x.Name)).ToList(); var token = _tokenProvider.Get(query.Username, claims); return(new AuthenticationResponse { Token = token, Username = user.Username }); }
public static async Task SeedAsync(IAppDatabaseContext appDatabaseContext, IConfiguration _) { var user = await appDatabaseContext.Set <User>().FirstOrDefaultAsync(x => x.Username == "jpinto3"); user ??= new User("johndoe", "*****@*****.**", "Qwertyuiop123!"); appDatabaseContext.Store(user); appDatabaseContext.SaveChangesAsync(default).GetAwaiter().GetResult();