Exemplo n.º 1
0
        public async Task <AuthenticationModel> GetTokenAsync(GetTokenQuery query)
        {
            var authenticationModel = new AuthenticationModel();
            var user = await _userManagerWrapper.FindByEmailAsync(query.Email);

            if (user == null)
            {
                throw new IncorrectCredentialsException(query.Email);
            }

            if (!await _userManagerWrapper.CheckPasswordAsync(user, query.Password))
            {
                throw new IncorrectCredentialsException(query.Email);
            }

            authenticationModel.IsAuthenticated = true;
            var jwtSecurityToken = await CreateJwtToken(user);

            authenticationModel.Token    = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
            authenticationModel.Email    = user.Email;
            authenticationModel.UserName = user.UserName;
            var rolesList = await _userManagerWrapper.GetRolesAsync(user).ConfigureAwait(false);

            authenticationModel.Roles = rolesList.ToList();
            return(authenticationModel);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Authenticate([FromBody] UserModel userModel)
        {
            if (!string.IsNullOrEmpty(userModel.UserName))
            {
                var user = await _userManager.FindByNameAsync(userModel.UserName);

                if (user != null)
                {
                    var correctPassword = await _userManager.CheckPasswordAsync(user, userModel.Password);

                    if (correctPassword)
                    {
                        var jwtToken = GenerateJwtToken(user);
                        return(Ok(new { token = jwtToken }));
                    }
                }
            }

            return(BadRequest());
        }