Ejemplo n.º 1
0
        /// <summary>
        /// 初始化Token
        /// </summary>
        /// <returns></returns>
        private async Task InitTokenIfNullTokenAsync()
        {
            try
            {
                if (this.tokenResult == null)
                {
                    this.tokenResult = await this.RequestTokenResultAsync();

                    var dueTime = this.GetDueTimeSpan(this.tokenResult.ExpiresIn);
                    this.tokenTimer.Change(dueTime, Timeout.InfiniteTimeSpan);
                }
            }
            catch (Exception ex)
            {
                this.tokenException = ex;
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 刷新token
 /// </summary>
 /// <returns></returns>
 private async Task RefreshTokenAsync()
 {
     try
     {
         if (string.IsNullOrEmpty(this.tokenResult.RefreshToken) == true)
         {
             this.tokenResult = await this.RequestTokenResultAsync();
         }
         else
         {
             this.tokenResult = await this.RequestRefreshTokenAsync(this.tokenResult.RefreshToken);
         }
     }
     catch (Exception ex)
     {
         this.tokenException = ex;
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 初始化或刷新token
 /// </summary>
 /// <returns></returns>
 private async Task InitOrRefreshTokenAsync()
 {
     if (this.token == null)
     {
         this.token = await this.RequestTokenResultAsync().ConfigureAwait(false);
     }
     else if (this.token.IsExpired() == true)
     {
         if (this.token.CanRefresh() == true)
         {
             this.token = await this.RequestRefreshTokenAsync(this.token.RefreshToken).ConfigureAwait(false);
         }
         else
         {
             this.token = await this.RequestTokenResultAsync().ConfigureAwait(false);
         }
     }
     this.token.EnsureSuccess();
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 应用AccessToken
 /// 默认为添加到请求头的Authorization
 /// </summary>
 /// <param name="context">请求上下文</param>
 /// <param name="tokenResult">token结果</param>
 protected virtual void AccessTokenResult(ApiActionContext context, TokenResult tokenResult)
 {
     context.RequestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokenResult.AccessToken);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 清除Token
 /// 迫使下次请求将重新获取token
 /// </summary>
 protected void ClearToken()
 {
     this.token = null;
 }