Example #1
0
        public async Task <JwtResult> LogGebruikerIn(LogGebruikerInCommand logGebruikerInCommand)
        {
            var creds = logGebruikerInCommand.Credentials;

            var user = await _userManager.Users.SingleOrDefaultAsync(usr => usr.UserName == creds.UserName);

            if (user == null)
            {
                _logger.LogInformation("Failed login for {user}", creds.UserName);
                throw new LoginFailedException("Wrong username or password.");
            }

            var result = await _signinManager.CheckPasswordSignInAsync(user, creds.Password, false);

            if (result.Succeeded)
            {
                var roles = await _userManager.GetRolesAsync(user);

                return(new JwtResult()
                {
                    Token = JwtUtil.Generate(user, roles)
                });
            }
            else
            {
                _logger.LogInformation("Failed login for {user}", creds.UserName);
                throw new LoginFailedException("Wrong username or password.");
            }
        }