Ejemplo n.º 1
0
        public async Task <ResultModel> RefreshMyToken()
        {
            var token  = new Token();
            var result = new ResultModel();

            try
            {
                //從Cookie取出token
                var Get_access_token = new ResultModel();
                Get_access_token = _cookieHelper.Get("access_token");
                if (Get_access_token.IsSuccess)
                {
                    token.access_token = Get_access_token.Data.ToString();
                }
                var Get_refresh_token = new ResultModel();
                Get_refresh_token = _cookieHelper.Get("refresh_token");
                if (Get_refresh_token.IsSuccess)
                {
                    token.refresh_token = Get_refresh_token.Data.ToString();
                }
                var tokenHandler = new JwtSecurityTokenHandler();

                var jwtToken = tokenHandler.ReadToken(token.access_token) as JwtSecurityToken; // 將字符串token解碼成token對象
                result.IsSuccess = false;
                result.Message   = "Token unauthorized.";
                var tokenExpireTime = jwtToken.ValidTo.ToLocalTime();
                if (tokenExpireTime < DateTime.Now)
                {
                    var data = JsonConvert.SerializeObject(token);
                    result = await CallAPI(data, new Uri(_config["api"].ToString() + "/auth/RefreshToken"), "POST");

                    if (!result.IsSuccess)
                    {
                        return(result);
                    }
                    else
                    {
                        var newToken = JsonConvert.DeserializeObject <Token>(result.Data.ToString());
                        //建立Cookie
                        double LoginExpireMinute   = Convert.ToDouble(_config["LoginExpireMinute"]);
                        var    Remove_access_token = new ResultModel();
                        Remove_access_token = _cookieHelper.Remove("access_token");
                        var Remove_refresh_token = new ResultModel();
                        Remove_refresh_token = _cookieHelper.Remove("refresh_token");
                        if (Remove_access_token.IsSuccess == false || Remove_refresh_token.IsSuccess == false)
                        {
                            result.IsSuccess = false;
                            result.Message   = "Delete cookies fail.";
                            return(result);
                        }
                        _cookieHelper.Remove("refresh_token");
                        CookieOptions cookieOptions = new CookieOptions();
                        cookieOptions.Expires = new DateTimeOffset(DateTime.Now.AddMinutes(LoginExpireMinute));
                        var Set_access_token = new ResultModel();
                        Set_access_token = _cookieHelper.Set("access_token", newToken.access_token, cookieOptions);
                        var Set_refresh_token = new ResultModel();
                        Set_refresh_token = _cookieHelper.Set("refresh_token", newToken.refresh_token, cookieOptions);
                        if (!Set_access_token.IsSuccess || !Set_refresh_token.IsSuccess)
                        {
                            throw new Exception("Write cookies error.");
                        }
                    }
                }
            }
            catch (Exception e) {
                result.Message = e.Message;
            }

            return(result);
        }