2011-05-20 62 views
5

這是可以在ASP.Net中爲不同會話設置不同的超時時間嗎?在ASP.Net中爲不同會話變量設置不同的超時時間。

編輯 我的意思是在同一個頁面我有2個會話變量會話[「SS1」]和會話[「SS2」],有可能設置超時爲每個會話?或者有沒有辦法像保存會話cookie一樣並設置過期? Sry基因即時通訊只是新的ASP.Net

+0

需要更多信息來了解您的問題 – jams 2011-05-20 09:00:19

+0

您的意思是什麼'不同'? – jams 2011-05-20 09:00:57

+0

您是否檢查過:http://forums.asp.net/t/1563991.aspx/1? – NaveenBhat 2011-05-20 09:01:22

回答

0

如果你談論的是針對不同的用戶會話超時然後 可以使用Global.asax在此,您可以使用Session_Start事件,在這個事件中,你可以爲不同的用戶

設置不同會話超時
3

設置在登錄時任何超時,可以爲不同的用戶設置不同的超時......

HttpContext.Current.Session.Timeout = 540; 
+0

KamalaPrakash它我設置了會話超時。它不會再看web.config嗎? – levi 2013-02-08 11:53:56

0

答案是否定的會話超時適用於每個用戶的所有會話變量。但是,您可以使用緩存或Cookie,它們都支持個人(每個密鑰)級別的超時。

但是,堅持這些解決方案並不沒有一些主要缺點。如果使用緩存,會失去會話提供的隱私,如果使用cookie,則會受到文件大小和序列化問題的限制。

解決此問題的一個解決方法是使用緩存並確保將用戶的會話ID包含在您使用的每個密鑰中。這樣你最終將得到一個模仿會話本身的緩存存儲。

如果您想了解更多的功能,不想理會實現這個但是你可以從CodePlex上的這個小項目中使用API​​:

http://www.univar.codeplex.com

2.0版本提供了許多存儲類型的選擇了包含會話綁定緩存的框。

6

我寫了一個非常簡單的擴展類來做到這一點。你可以找到源代碼here

用法:

//store and expire after 5 minutes 
Session.AddWithTimeout("key", "value", TimeSpan.FromMinutes(5)); 
0
/// <summary> 
/// this class saves something to the Session object 
/// but with an EXPIRATION TIMEOUT 
/// (just like the ASP.NET Cache) 
/// (c) Jitbit 2011. MIT license 
/// usage sample: 
/// Session.AddWithTimeout(
/// "key", 
/// "value", 
/// TimeSpan.FromMinutes(5)); 
/// </summary> 
public static class SessionExtender 
{ 
    public static void AddWithTimeout(
    this HttpSessionState session, 
    string name, 
    object value, 
    TimeSpan expireAfter) 
    { 
    session[name] = value; 
    session[name + "ExpDate"] = DateTime.Now.Add(expireAfter); 
    } 

    public static object GetWithTimeout(
    this HttpSessionState session, 
    string name) 
    { 
    object value = session[name]; 
    if (value == null) return null; 

    DateTime? expDate = session[name + "ExpDate"] as DateTime?; 
    if (expDate == null) return null; 

    if (expDate < DateTime.Now) 
    { 
     session.Remove(name); 
     session.Remove(name + "ExpDate"); 
     return null; 
    } 

    return value; 
    } 
} 
Usage: 

//store and expire after 5 minutes 
Session.AddWithTimeout("key", "value", TimeSpan.FromMinutes(5)); 

//get the stored value 
Session.GetWithTimeout("key"); 

亞歷克斯。首席執行官,創始人https://www.jitbit.com/alexblog/196-aspnet-session-caching-expiring-values/

相關問題