コード例 #1
0
 public async Task <bool> LoginAsync(string userName, string password) =>
 (await OAuthClient.GetAccessToken(userName, password))
 .OnFailure(result =>
 {
     GetAccessTokenFailed?.Invoke(result.Error);
     _authParameters = AuthParameters.Empty;
 })
 .OnSuccess(result => GetAccessTokenSucceed?.Invoke(result.Value.AccessToken.Value))
 .OnSuccess(result => _authParameters = AuthParameters.Create(
                accessToken: result.Value.AccessToken.Value,
                accessTokenExpiration: result.Value.AccessToken.ExpireInSeconds,
                refreshToken: result.Value.RefreshToken.Value))
 .OnSuccess(() =>
 {
     _refreshCancellationTokenSource = new CancellationTokenSource();
     BeginTokenRefreshLoop(_refreshCancellationTokenSource.Token);
 }).IsSuccess;
コード例 #2
0
 private void BeginTokenRefreshLoop(CancellationToken ct)
 {
     _loopReference = Task.Run(async() =>
     {
         TokenRefreshLoopStatusChanged?.Invoke(RefreshLoopStatus.Started, LoopTimeout);
         try
         {
             while (true)
             {
                 await Task.Delay(LoopTimeout, ct);
                 (await RefreshAccessToken(_authParameters.RefreshToken))
                 .OnSuccess(() => TokenRefreshLoopStatusChanged?.Invoke(RefreshLoopStatus.Running, LoopTimeout))
                 .OnSuccess(result =>
                 {
                     _authParameters = result.Value;
                     GetAccessTokenSucceed?.Invoke($"Access Token Refreshed: {result.Value.AccessToken}");
                 })
                 .OnFailure(result =>
                 {
                     GetAccessTokenFailed?.Invoke(result.Error);
                     _refreshCancellationTokenSource.Cancel();
                 });
             }
         }
         catch (OperationCanceledException)
         {
             //no-op: refresh loop manually stopped, all ok.
         }
         catch (Exception exception)
         {
             GetAccessTokenFailed?.Invoke(exception.Message);
             _refreshCancellationTokenSource.Cancel();
         }
         finally
         {
             TokenRefreshLoopStatusChanged?.Invoke(RefreshLoopStatus.Stopped, LoopTimeout);
         }
     }, ct);
 }