public IActionResult Authenticate([FromBody] LoginInfo loingInfo)
        {
            var result = _userManager.Authenticate(loingInfo.Username, loingInfo.Password);

            if (!result.Success)
            {
                throw new ApiException(result.ErrorMessage, HttpStatusCode.Unauthorized);
            }

            if (result.User == null)
            {
                throw new ApiException("Unable to get user data", HttpStatusCode.Unauthorized);
            }

            var claims = new[]
            {
                new Claim(ClaimTypes.Name, result.User.Username),
                new Claim(ClaimTypes.Surname, result.User.Fullname)
            };

            return(Ok(new
            {
                token = AuthHelper.BuildToken(claims)
            }));
        }
Пример #2
0
        public IActionResult GetToken([FromBody] JwtAuthRequest authData)
        {
            IActionResult response = Unauthorized();

            var user = AuthHelper.Authenticate(authData, this._usersService.GetUsers());

            if (user == null)
            {
                response = BadRequest(new { error = $"Invalid User: [{authData.Username}]" });
                return(response);
            }

            // Build JWT
            var tokenString = AuthHelper.BuildToken(user, _config["Jwt:Key"], _config["Jwt:Issuer"]);

            response = Ok(new JwtAuthResponse()
            {
                Token = tokenString
            });

            return(response);
        }
Пример #3
0
        public async Task <IActionResult> Login(LoginVM loginVM)
        {
            var account = await this.accountService.LoginAsync(loginVM.UserName, loginVM.PassWord);

            if (account == null || string.IsNullOrEmpty(account.UserCode))
            {
                return(this.BadRequest("Tên đăng nhập hoặc mập khẩu không đúng"));
            }

            var token = AuthHelper.BuildToken(account);

            this.Response.Cookies.Append("token", token);

            return(this.Ok(new
            {
                token = token,
                username = account.UserName,
                name = account.EmployeeName,
                department = account.DepartmentName,
                usercode = account.UserCode
            }));
        }