2012-03-31 164 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