2012-03-31 168 views
1
<configuration> 
<connectionStrings> 
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"providerName="System.Data.SqlClient"/> 
</connectionStrings> 
<system.web> 
    <sessionState timeout="1" mode="InProc" cookieless="false" > 
    </sessionState> 
    <compilation debug="true" targetFramework="4.0"/> 
<authentication mode="Forms"> 
    <forms loginUrl="~/Account/Login.aspx" timeout="1"/> 
</authentication> 

我創建了一個會話,如果用戶進行身份驗證這樣會話變量

if (HttpContext.Current.User.Identity.IsAuthenticated) 
    { 

     string name = HttpContext.Current.User.Identity.Name.ToString(); 
     Session["username"] = name; 
    } 

然後我檢查,如果會話仍然像這裏一樣退出

if (Session["username"] == null) 
      Response.Redirect("SessionExpired.aspx"); 
     else 
      Response.Write("session still exist"); 

**問題:** 1分鐘後會話不變爲空,用戶始終登錄任何幫助

這是代碼:

 Welcome to ASP.NET! test1 
    <p> 
    To visit test2 go to<asp:LinkButton ID="LinkButton1" runat="server" 
     onclick="LinkButton1_Click">here</asp:LinkButton> 
    </p> 
    <asp:Label ID="msg" runat="server" Text=""></asp:Label> 

在服務器端

protected void LinkButton1_Click(object sender, EventArgs e) 
    { 
    if (HttpContext.Current.User.Identity.IsAuthenticated) 
    { 

    string name = HttpContext.Current.User.Identity.Name.ToString(); 
    Session["username"] = name; 
    if (Session["username"] == null) 
     Response.Redirect("~/SessionExpired.aspx"); 
    else 

msg.Text = "session still exist the session will be timedout after "+Session.Timeout+ "min"; 
} 
else 
    Response.Write("please login"); 
} 

回答

2
+1

你甚至讀過他的問題嗎? mode =「InProc」!!! – Baz1nga 2012-03-31 08:32:32

+0

是的。在我發佈的用戶鏈接也改變了模式「inproc」 – TRR 2012-03-31 08:35:15

+0

這是正確的,在這裏,他不是說他得到它後,改變它的InProc罰款..他面臨的問題,同時使用它.. – Baz1nga 2012-03-31 08:37:21

0

@bassem ala:雖然默認情況下會在配置文件中設置會話超時期限,但asp.net會檢查服務器會話超時值。您必須在一分鐘後手動將會話值設置爲null。

+0

我該如何做到這一點? – 2012-03-31 11:51:43

1

沒有機會將Session [「username」]對象設置爲null。所以如果陳述沒用..

string name = HttpContext.Current.User.Identity.Name.ToString(); 
    Session["username"] = name; 
    if (Session["username"] == null) 
     Response.Redirect("~/SessionExpired.aspx"); 
+0

如何檢查會話是否過期?如果(會話[「用戶名」] ==空) 是不是說如果會話變量已過期:? – 2012-03-31 11:51:28

+0

您可以在爲會話分配值之前檢查會話過期。不在之後。我的意思是,如果語句應該高於Session [「username」] = name;線。 – hkutluay 2012-03-31 12:04:12