2016-05-25 38 views
0

我創建使用OAuth認證承載如下的的WebAPI:絕對和空閒會話超時owin的WebAPI

 var oAuthServerOptions = new OAuthAuthorizationServerOptions 
     { 
      TokenEndpointPath = new PathString("/token"), 
      AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(100), 
      Provider = new AuthorizationServerProvider(), 
      RefreshTokenProvider = new RefreshTokenProvider(), 
     }; 

應用程序將生成身份驗證的用戶,將100分鐘後過期的令牌。用戶必須使用刷新令牌來繼續訪問應用程序。 現在,我想改變政策如下:

  • 如果用戶處於閒置狀態100分鐘,則用戶必須再次登錄(應用程序必須返回401) - 空閒超時
  • 事件,如果用戶不閒着,用戶必須在8小時後再次登錄 -

...我已經尋找這幾天絕對超時,但無法找到該

任何合適的解決方案是否有對工作的任何解決方案或樣品我問題在這裏? 目前,我刪除了刷新標記功能,以便用戶在100分鐘後必須再次登錄。

非常感謝。

回答

0

我沒有看到在OAuth 2.0中同時有兩個超時的方法。

僅關於第一個超時,空閒超時,您可以將刷新令牌超時設置爲100分鐘。訪問令牌超時將會減少,每次訪問令牌到期時,您都將獲得新的訪問權限和刷新令牌。如果用戶會話空閒超過100分鐘,當應用程序嘗試刷新令牌時,oauth服務器將認識到刷新令牌已過期並且無效。然後用戶需要輸入他們的憑證。

對於第二次超時,您可以將訪問令牌超時設置爲8小時,並且不實施刷新令牌。

考慮到令牌將被髮送到資源服務器,它不能與oauth服務器相同。資源服務器只能檢查到令牌的票證未過期,但無法控制在用戶輸入憑據後第一次授予令牌的時間。

如果您同時控制oauth和資源服務器,則可以爲刷新令牌執行100分鐘超時的解決方法,並在用戶輸入憑據時在屬性中包含屬性。請看下面的代碼爲例:

public class AuthorizationServerProvider : OAuthAuthorizationServerProvider 
{ 
    ... 
    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
     ... 
     var props = new AuthenticationProperties(new Dictionary<string, string> 
     { 
      { 
       "client_id", clientId 
      }, 
      { 
       "ownerCredentialsTimestamp", DateTime.UtcNow.ToString() 
      } 
     }); 

     var ticket = new AuthenticationTicket(identity, props); 
     context.Validated(ticket); 
    } 
    ... 
} 

當資源服務器獲取包含在令牌中的票,可以在屬性與當前時間比較值的。在差異的情況下超過8小時大可以返回一個401 - 未授權響應,迫使客戶端應用程序,要求另一訪問令牌:

public class AccessTokenProvider : IAuthenticationTokenProvider 
{ 
    public async Task ReceiveAsync(AuthenticationTokenReceiveContext context) 
    { 
     context.DeserializeTicket(context.Token); 

     if (context.Ticket.Properties.Dictionary["ownerCredentialsTimestamp"] != null) 
     { 
      var ownerCredentialsTimestamp = Convert.ToDateTime(context.Ticket.Properties.Dictionary["ownerCredentialsTimestamp"]).ToUniversalTime(); 

      if (/* difference is bigger than 8 hours */) 
      { 
       context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; 
      } 
     } 
    } 
} 

在這一點上,客戶端應用程序將嘗試獲得一個新的使用「refresh_token」請求訪問令牌。 oauth服務器必須再次檢查上次輸入的與當前刷新令牌相關的憑證的時間,數據庫表中可能存在一個存儲刷新令牌的列(如果這是您的情況)。

你可以在RefreshTokenProvider.ReceiveAsync()方法檢查:

public class RefreshTokenProvider : IAuthenticationTokenProvider 
{ 
    ... 
    public async Task ReceiveAsync(AuthenticationTokenReceiveContext context) 
    { 
     ... 
     /* Check the received refresh token, including the last time that the credentials were entered for this user */ 
     ... 
    } 
    ... 
} 

或者在AuthorizationServerProvicer.GrantRefreshToken()方法:

public class AuthorizationServerProvider : OAuthAuthorizationServerProvider 
{ 
    ... 
    public override async Task GrantRefreshToken(OAuthGrantRefreshTokenContext context) 
    { 
     ... 
     /* Check the last time that the credentials were entered for this user */ 
     ... 
    } 
    ... 
} 

這是沒有任何使用OAuth 2.0協議非常具體的解決方案。

我希望它能幫助你。

+0

您的解決方案聽起來非常好。我會檢查我目前的源代碼,並會盡快回復。非常感謝你。 :) –

+0

你的答案給了我一個想法來完成我的問題。韓國社交協會。 –