[英]How to Check whether Session is Expired or not in asp.net
我已經在 web.config 文件中指定了會話超時。 當會話超時時,我沒有重定向到登錄頁面,但我收到一條錯誤消息,指出未將對象引用設置為實例。
誰能告訴我這個問題的解決方案?
您可以檢查HttpContext.Current.User.Identity.IsAuthenticated
屬性,這將使您知道當前是否有經過身份驗證的用戶。
編輯
您可以使用 IsNewSession 屬性來檢查會話是否是在頁面請求時創建的
protected void Page_Load()
{
if (Context.Session != null)
{
if (Session.IsNewSession)
{
string cookieHeader = Request.Headers["Cookie"];
if ((null != cookieHeader) && (cookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
{
Response.Redirect("sessionTimeout.htm");
}
}
}
}
預
當用戶登錄網站並檢查您的母版頁或創建的其他頁面繼承的基頁表單時,將Userid
存儲在會話變量中。 然后在頁面加載中檢查Userid
是否存在,如果不存在則重定向到登錄頁面。
if(Session["Userid"]==null)
{
//session expire redirect to login page
}
我不喜歡在代碼中檢查會話變量,而是使用 FormAuthentication。 它們具有內置功能,可以重定向到 web.config 中指定的給定 LoginPage。
但是,如果您想明確檢查會話,您可以檢查您之前在會話中創建的任何變量的 NULL 值,正如 Pranay 回答的那樣。
您可以創建 Login.aspx 頁面並在那里寫下您的消息,當會話過期時 FormAuthentication 自動重定向到 FormAuthentication 部分中給出的 loginUrl
<authentication mode="Forms">
<forms loginUrl="Login.aspx" protection="All" timeout="30">
</forms>
</authentication>
問題是您不能為 Login 和 SessionExpire 提供單獨的頁面,因此您必須顯示/隱藏 Login.aspx 上的某些部分以雙向操作。
還有另一種方法可以在超時后重定向到 sessionexpire 頁面而不更改 formauthentication->loginurl ,請參閱以下鏈接: http : //www.schnieds.com/2009/07/aspnet-session-expiration-redirect.html
使用Session.Contents.Count
:
if (Session.Contents.Count == 0)
{
Response.Write(".NET session has Expired");
Response.End();
}
else
{
InitializeControls();
}
上面的代碼假設您至少在用戶第一次訪問您的網站時創建了一個會話變量。 如果您沒有,那么您很可能沒有為您的應用程序使用數據庫。 對於您的情況,您可以使用以下示例手動分配會話變量。
protected void Page_Load(object sender, EventArgs e)
{
Session["user_id"] = 1;
}
祝你好運!
檢查它是否為空,例如
if(Session["mykey"] != null)
{
// Session is not expired
}
else
{
//Session is expired
}
我使用@Adi-lester 答案並添加了一些方法。
驗證Session是否處於活動狀態的方法
public static void SessionIsAlive(HttpSessionStateBase Session)
{
if (Session.Contents.Count == 0)
{
Response.Redirect("Timeout.html");
}
else
{
InitializeControls();
}
}
在頁面加載中創建會話變量
protected void Page_Load(object sender, EventArgs e)
{
Session["user_id"] = 1;
}
創建 SaveData 方法(但您可以在所有方法中使用它)
protected void SaveData()
{
// Verify if Session is Alive
SessionIsAlive(Session);
//Save Data Process
// bla
// bla
// bla
}
通過這種方式,許多人檢測會話是否已過期。 下面的代碼可以幫助你。
protected void Page_Init(object sender, EventArgs e)
{
if (Context.Session != null)
{
if (Session.IsNewSession)
{
HttpCookie newSessionIdCookie = Request.Cookies["ASP.NET_SessionId"];
if (newSessionIdCookie != null)
{
string newSessionIdCookieValue = newSessionIdCookie.Value;
if (newSessionIdCookieValue != string.Empty)
{
// This means Session was timed Out and New Session was started
Response.Redirect("Login.aspx");
}
}
}
}
}
在這里,我正在檢查會話值(在上一頁的文本框中填充的兩個值)
protected void Page_Load(object sender, EventArgs e)
{
if (Session["sessUnit_code"] == null || Session["sessgrcSerial"] == null)
{
Response.Write("<Script Language = 'JavaScript'> alert('Go to GRC Tab and fill Unit Code and GRC Serial number first')</script>");
}
else
{
lblUnit.Text = Session["sessUnit_code"].ToString();
LblGrcSr.Text = Session["sessgrcSerial"].ToString();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.