コード例 #1
0
ファイル: AuthController.cs プロジェクト: luanvaf/ProjetoLES
        public async Task <IActionResult> Login([FromBody] DtoCreateAuthInput authInput)
        {
            var createAuthResponse = await _createAuthService.Execute(authInput);

            if (createAuthResponse.Success)
            {
                return(Ok(createAuthResponse.Value));
            }

            return(BadRequest(createAuthResponse.Message));
        }
コード例 #2
0
        public async Task <ResponseService <DtoCreateAuthResponse> > Execute(DtoCreateAuthInput dtoCreateAuth)
        {
            var existingAuth = await _userRepository.GetByLogin(dtoCreateAuth.Login);

            if (existingAuth != null)
            {
                var correctPassword = _cryptograph.VerifyPassword(dtoCreateAuth.Password, existingAuth.Password);

                if (correctPassword)
                {
                    var tokenHandler = new JwtSecurityTokenHandler();
                    var key          = Encoding.ASCII.GetBytes(_jwtSecretKey);

                    var tokenDescriptor = new SecurityTokenDescriptor
                    {
                        Subject = new ClaimsIdentity(new Claim[]
                        {
                            new Claim(ClaimTypes.Name, existingAuth.Name),
                            new Claim(ClaimTypes.Role, existingAuth.RoleId.ToString()),
                            new Claim("UserId", existingAuth.Id.ToString()),
                        }),
                        Expires = DateTime.UtcNow.AddHours(3),

                        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                    };

                    var token = tokenHandler.CreateToken(tokenDescriptor);

                    var user = new DtoUser
                    {
                        CompleteName = existingAuth.Name,
                    };

                    var authResult = new DtoCreateAuthResponse {
                        User = user, Token = tokenHandler.WriteToken(token), Role = existingAuth.RoleId.ToString()
                    };


                    return(GenerateSuccessServiceResponse(authResult));
                }
            }
            return(GenerateErroServiceResponse <DtoCreateAuthResponse>("Email ou senha invalidos."));
        }