当前位置:编程学习 > C#/ASP.NET >>

asp.net session 会话层设置结束时间设置

 Session是什么呢?简单来说就是服务器给客户端的一个编号。当一台WWW服务器运行时,可能有若干个用户浏览正在运正在这台服务器上的网站。当每个用户首次与这台WWW服务器建立连接时,他就与这个服务器建立了一个Session,同时服务器会自动为其分配一个SessionID,用以标识这个用户的唯一身份。这个SessionID是由WWW服务器随机产生的一个由24个字符组成的字符串,我们会在下面的实验中见到它的实际样子。

  这个唯一的SessionID是有很大的实际意义的。当一个用户提交了表单时,浏览器会将用户的SessionID自动附加在HTTP头信息中,(这是浏览器的自动功能,用户不会察觉到),当服务器处理完这个表单后,将结果返回给SessionID所对应的用户

如果一个会话结束

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.asp教程x.cs" Inherits="UsingSessionEnd" EnableSessionState="ReadOnly" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>Using Session State - End</title>
</head>
<body>
  <form id="form1" runat="server">
    <div id="container">
      <h1>Using Session State - End</h1>
           Session value:
            <asp:Label ID="labSession" runat="server" /><br />
            <asp:HyperLink ID="lnkSession" runat="server" NavigateUrl="UsingSessionStart.aspx" Text="Go to session start page" /><br />
     </div>
  </form>
</body>
</html>

File: Default.aspx.cs

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UsingSessionEnd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (Session["Name"] == null)
          labSession.Text = "Session has timed out or not been initialized";
       else
       {
          string name = (string)Session["Name"];
          labSession.Text = name;
       }
    }
}

使用Cookieless会话状态


The web configuration file enables cookieless sessions by assigning the value AutoDetect to the cookieless attribute.

File: Web.Config

<configuration>
<system.web>
  <sessionState
    cookieless="AutoDetect"
    regenerateExpiredSessionId="true" />
</system.web>
</configuration>

session超时时间

File: Web.Config

<configuration>
<system.web>

  <sessionState timeout="60" />

</system.web>
</configuration>


You can modify the Session timeout value programmatically with the Timeout property of the Session object.


Session.Timeout = 60;

补充:asp.net教程,基础入门 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,