public async Task GetAsyncCheckWhenRefreshTokenNull_Test()
        {
            _refreshTokenRepositoryMock.Setup(repo => repo.GetAsync(It.IsAny <System.Linq.Expressions.Expression <Func <RefreshTokens, bool> > >())).Returns(async() => { return(null); });

            HttpStatusCodeException ex = await Assert.ThrowsExceptionAsync <HttpStatusCodeException>(() => _refreshTokenService.GetAsync(RefreshToken));

            Assert.AreEqual("Such refresh token doesn't exist", ex.Message);
        }
예제 #2
0
        public static async Task <bool> CheckAndRefreshToken(IPageDialogService dialogService,
                                                             RefreshTokenService refreshTokenService, SystemStatusService systemStatusService,
                                                             AppStatus appStatus)
        {
            if (appStatus.SystemStatus.TokenExpireDatetime > DateTime.Now)
            {
                #region Token 尚在有效期限
                return(true);

                #endregion
            }
            else
            {
                #region Token 已經失效了,需要更新
                var fooResult = await refreshTokenService.GetAsync();

                if (fooResult.Status != true)
                {
                    await dialogService.DisplayAlertAsync("發生錯誤", fooResult.Message, "確定");

                    return(false);
                }
                systemStatusService.SingleItem                        = appStatus.SystemStatus;
                systemStatusService.SingleItem.IsLogin                = true;
                systemStatusService.SingleItem.LoginedTime            = DateTime.Now;
                systemStatusService.SingleItem.Token                  = refreshTokenService.SingleItem.Token;
                systemStatusService.SingleItem.RefreshToken           = refreshTokenService.SingleItem.RefreshToken;
                systemStatusService.SingleItem.TokenExpireMinutes     = refreshTokenService.SingleItem.TokenExpireMinutes;
                systemStatusService.SingleItem.RefreshTokenExpireDays = refreshTokenService.SingleItem.RefreshTokenExpireDays;
                systemStatusService.SingleItem.SetExpireDatetime();
                #endregion
            }

            //await systemStatusService.WriteToFileAsync();
            await AppStatusHelper.WriteAndUpdateAppStatus(systemStatusService, appStatus);

            return(true);
        }
예제 #3
0
        static async Task Main(string[] args)
        {
            LoginManager loginManager = new LoginManager();

            Output("進行登入身分驗證");
            APIResult result = await loginManager.PostAsync(new LoginRequestDTO()
            {
                Account  = "user50",
                Password = "******"
            });

            if (result.Status == true)
            {
                Console.WriteLine($"登入成功");
                Console.WriteLine($"{result.Payload}");
            }
            else
            {
                Console.WriteLine($"登入失敗");
                Console.WriteLine($"{result.Message}");
            }
            Thread.Sleep(2000);

            Output("利用取得的 JTW Token 呼叫取得部門資訊 Web API");
            DepartmentsManager departmentsManager = new DepartmentsManager();

            result = await departmentsManager.GetAsync(loginManager.SingleItem.Token);

            if (result.Status == true)
            {
                Console.WriteLine($"取得部門資料成功");
                Console.WriteLine($"{result.Payload}");
            }
            else
            {
                Console.WriteLine($"取得部門資料失敗");
                Console.WriteLine($"{result.Message}");
            }

            Console.WriteLine("等候10秒鐘,等待 JWT Token 失效");
            await Task.Delay(10000);

            departmentsManager = new DepartmentsManager();
            Output("再次呼叫取得部門資訊 Web API,不過,該 JWT Token已經失效了");
            result = await departmentsManager.GetAsync(loginManager.SingleItem.Token);

            if (result.Status == true)
            {
                Console.WriteLine($"取得部門資料成功");
                Console.WriteLine($"{result.Payload}");
            }
            else
            {
                Console.WriteLine($"取得部門資料失敗");
                Console.WriteLine($"{result.Message}");
            }
            Thread.Sleep(2000);

            RefreshTokenService refreshTokenService = new RefreshTokenService();

            Output("呼叫更新 JWT Token API,取得更新的 JWT Token");
            result = await refreshTokenService.GetAsync(loginManager.SingleItem.RefreshToken);

            if (result.Status == true)
            {
                Console.WriteLine($"更新 JWT Token 成功");
                Console.WriteLine($"{result.Payload}");
            }
            else
            {
                Console.WriteLine($"更新 JWT Token 失敗");
                Console.WriteLine($"{result.Message}");
            }
            Thread.Sleep(2000);

            departmentsManager = new DepartmentsManager();
            Output("再次呼叫取得部門資訊 Web API,不過,使用剛剛取得的更新 JWT Token");
            result = await departmentsManager.GetAsync(refreshTokenService.SingleItem.Token);

            if (result.Status == true)
            {
                Console.WriteLine($"取得部門資料成功");
                Console.WriteLine($"{result.Payload}");
            }
            else
            {
                Console.WriteLine($"取得部門資料失敗");
                Console.WriteLine($"{result.Message}");
            }
            Thread.Sleep(2000);

            Console.WriteLine("Press any key for continuing...");
            Console.ReadKey();
        }