Пример #1
0
        public ApiResult <TokenResultDto> SignIn(string UserId, string Password)
        {
            // 以下變數值應該透過 IConfiguration 取得
            var issuer  = _configuration["JWT:issuer"].ToString();        //"JwtAuthDemo";
            var signKey = _configuration["JWT:signKey"].ToString();       // 請換成至少 16 字元以上的安全亂碼
            var expires = Convert.ToInt32(_configuration["JWT:expires"]); // 單位: 分鐘

            TokenResultDto             tokenResultDto;
            ApiResult <TokenResultDto> status = new ApiResult <TokenResultDto>();

            status.State = false;
            if (_userValidateService.ValidateUser(UserId, Password))
            {
                var refreshToken = Guid.NewGuid().ToString();
                _refreshTokenService.InsertRefreshToken(UserId, refreshToken);

                tokenResultDto = new TokenResultDto()
                {
                    accessToken  = JwtHelpers.GenerateToken(issuer, signKey, UserId, expires, _userInfoService.GetApiRoles(UserId), JsonConvert.SerializeObject(_userInfoService.GetUserInfo(UserId))),
                    refreshToken = refreshToken
                };

                status.State  = true;
                status.Result = tokenResultDto;
            }
            else
            {
                status.Message = "帳號密碼輸入錯誤";
            }

            return(status);
        }
Пример #2
0
        public ActionResult <string> SignIn(LoginViewModel login)
        {
            // 以下變數值應該透過 IConfiguration 取得
            var issuer  = _configuration["JWT:issuser"].ToString();       //"JwtAuthDemo";
            var signKey = _configuration["JWT:signKey"].ToString();       // 請換成至少 16 字元以上的安全亂碼
            var expires = Convert.ToInt32(_configuration["JWT:expires"]); // 單位: 分鐘

            if (_userValidateService.ValidateUser(login))
            {
                return(JwtHelpers.GenerateToken(issuer, signKey, login.Username, expires, _userInfoService.GetApiRoles(login.Username), JsonConvert.SerializeObject(_userInfoService.GetUserInfo(login.Username))));
            }
            else
            {
                return(BadRequest());
            }
        }