[英]How to stop extending cookie expiration time for ajax calls?
我將ExpireTimeSpan設置為一天,但是我的UI中有通知服務,該服務每5分鍾發送一次ajax調用,因此ajax調用會延長有效期,因此,如果用戶是,則系統將在一段時間后不注銷在積極,我該如何解決
int expireTime = 1440; //one day
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = TimeSpan.FromMinutes(expireTime),
SlidingExpiration=true,
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
我找到了用於asp.net核心的解決方案: https : //medium.com/cacti-pins/conditionally-set-sliding-expiration-time-on-authentication-cookies-in-asp-net-core-e70ffe7da49d,但我m使用.net4.5
對於該問題,我認為沒有特別簡單的解決方案,但是您可以考慮幾種方法。
首先,您可以考慮將WebSockets用於通知服務。 在C#應用程序中,使用SignalR似乎是明智的。 這將允許您打開客戶端與服務器之間的持久連接並啟用服務器推送,這只應包括在初始連接上發送cookie。 請注意,默認情況下,如果瀏覽器中不支持其他選項,則SignalR可以降級為長時間輪詢,而這仍然會遇到相同的問題。
另外,您可以考慮將ajax調用換成javascript fetch()
。 這樣,您可以在發出請求時忽略Cookie。 但是,您將需要在應用程序內生成備用授權令牌並進行管理。 您可能還需要將通知服務移至其自己的應用程序中,以便它不使用相同的身份驗證方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.