예제 #1
0
파일: Login.cs 프로젝트: Wallruzz9114/vanir
            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
                });
            }
예제 #2
0
        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();