共用方式為


<sessionState> Element

Configures session state settings for the current application.

<configuration>
   <system.web>
      <sessionState>

<sessionState mode="Off|InProc|StateServer|SQLServer"
              cookieless="true|false"
              timeout="number of minutes"
              stateConnectionString="tcpip=server:port"
              sqlConnectionString="sql connection string"
              stateNetworkTimeout="number of seconds"/>

Required Attributes

Attribute Option Description
mode     Specifies where to store the session state.
    Off Indicates that session state is not enabled.
    InProc Indicates that session state is stored locally.
    StateServer Indicates that session state is stored on a remote server.
    SQLServer Indicates that session state is stored on the SQL Server.

Optional Attributes

Attribute Option Description
cookieless     Specifies whether sessions without cookies should be used to identify client sessions.
    true Indicates that sessions without cookies should be used.
    false Indicates that sessions without cookies should not be used. The default is false.
timeout     Specifies the number of minutes a session can be idle before it is abandoned. The default is 20.
stateConnectionString     Specifies the server name and port where session state is stored remotely. For example, "tcpip=127.0.0.1:42424". This attribute is required when mode is StateServer.
sqlConnectionString     Specifies the connection string for a SQL Server. For example, "data source=localhost;Integrated Security=SSPI;Initial Catalog=northwind". This attribute is required when mode is SQLServer.
stateNetworkTimeout     When using StateServer mode to store session state, specifies the number of seconds the TCP/IP network connection between the Web server and the state server can be idle before the session is abandoned. The default is 10.

Remarks

To use StateServer mode

  1. Make sure ASP.NET state service is running on the remote server that will store session state information. This service is installed with ASP.NET and is located by default at <Drive>:\systemroot\Microsoft.NET\Framework\version\aspnet_state.exe.
  2. In the application's Web.config file, set mode=StateServer and set the stateConnectionString attribute. For example, stateConnectionString="tcpip=dataserver:42424".

To use SQLServer mode

  1. Run InstallSqlState.sql (installed by default in <Drive>:\systemroot\Microsoft.NET\Framework\version) on the computer running SQL Server that will store the session state. This creates a database called ASPState with new stored procedures and ASPStateTempApplications and ASPStateTempSessions tables in the TempDB database.
  2. In the application's Web.config file, set mode=SQLServer and set the sqlConnectionString attribute. For example, sqlConnectionString="data source=localhost;Integrated Security=SSPI;Initial Catalog=northwind".

Example

The following example specifies several session state configuration settings.

<configuration>
   <system.web>
      <sessionState mode="InProc"
                    cookieless="true"
                    timeout="20"/>
      </sessionState>
   </system.web>
</configuration>

Requirements

Contained Within: <system.web>

Web Platform: IIS 5.0, IIS 5.1, IIS 6.0

Configuration File: Machine.config, Web.config

Configuration Section Handler: System.Web.SessionState.SessionStateSectionHandler

See Also

ASP.NET Configuration | ASP.NET Settings Schema | SessionStateModule