OAuth2 WebApi 令牌過期
我正在嘗試動態設置令牌到期時間,但它似乎只是預設為 20 分鐘。
這是我的配置身份驗證:
public void ConfigureAuth(IAppBuilder app) { OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"), Provider = new ApplicationOAuthProvider(""), // In production mode set AllowInsecureHttp = false AllowInsecureHttp = true }; // Enable the application to use bearer tokens to authenticate users app.UseOAuthBearerTokens(OAuthOptions); }這是我的 GrantResourceOwnerCredentials 方法:
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); var hasValidLogin = (new login().authenticate(context.UserName, context.Password, "") == "valid"); if (hasValidLogin == false) { context.SetError("invalid_grant", "The user name or password is incorrect."); return Task.FromResult<object>(null); } var oAuthIdentity = CreateIdentity(context); var oAuthProperties = CreateProperties(context); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, oAuthProperties); context.Validated(ticket); return Task.FromResult<object>(null); }這是我可以設置到期時間的 SetProperties 方法:
public static AuthenticationProperties CreateProperties(OAuthGrantResourceOwnerCredentialsContext context) { IDictionary<string, string> data = new Dictionary<string, string> { { "client_id", context.ClientId } }; var response = new AuthenticationProperties(data); response.ExpiresUtc = DateTime.Now.AddMonths(1); return response; }即使在那之後,令牌也在返回:
{ "access_token": ".....", "token_type": "bearer", "expires_in": 1199, "client_id": ".....", ".expires": "Fri, 13 Nov 2015 20:24:06 GMT", ".issued": "Fri, 13 Nov 2015 20:04:06 GMT" }有什麼想法為什麼我不能在目前位置設置到期時間?該伺服器將採用具有不同指定到期時間的各種不同客戶端,因此我認為這是執行此操作的地方。還有其他地方我應該這樣做嗎?謝謝!
您看到的行為直接是由於 OAuth2 授權伺服器在您在通知中設置它時總是丟棄您自己的過期時間
GrantResourceOwnerCredentials(其他Grant*通知也受到影響):https ://github.com/jchannon/katanaproject/blob /master/src/Microsoft.Owin.Security.OAuth/OAuthAuthorizationServerHandler.cs#L386一種解決方法是在
AuthenticationTokenProvider.CreateAsync(您使用的類OAuthAuthorizationServerOptions.AccessTokenProvider)中設置到期日期:只需設置
context.Ticket.Properties.ExpiresUtc您選擇的到期日期,它應該可以按預期工作:public class AccessTokenProvider : AuthenticationTokenProvider { public override void Create(AuthenticationTokenCreateContext context) { context.Ticket.Properties.ExpiresUtc = // set the appropriate expiration date. context.SetToken(context.SerializeTicket()); } }您還可以查看
AspNet.Security.OpenIdConnect.ServerOWIN/Katana 提供的 OAuth2 授權伺服器的一個分支,它本機支持從以下位置設置到期日期GrantResourceOwnerCredentials:https ://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/tree /dev
我們有類似的情況,不同的客戶端有不同的令牌超時,所以我們希望能夠相應地設置過期時間。在我們實現的 AuthenticationTokenProvider 中,我們設置了過期時間,但在簽名令牌時它被覆蓋了。
我們最終滿意的解決方案是覆蓋 TokenEndpoint 方法。然後我們可以實現客戶端特定的過期:
public override Task TokenEndpoint(OAuthTokenEndpointContext context) { if (context.TokenIssued) { // client information var accessExpiration = DateTimeOffset.Now.AddSeconds(accessTokenTimeoutSeconds); context.Properties.ExpiresUtc = accessExpiration; } return Task.FromResult<object>(null); }*編輯以解決競爭條件。