ASP 會話會話 <>

概觀

專案的 <session> 元素 <asp> 會指定 ACTIVE Server Pages (ASP) 會話狀態設定。 會話狀態是 Internet Information Services (IIS) 7 儲存每個唯一用戶端會話的相關資訊的方法。 例如,如果您的網站有購物車應用程式,則每個用戶端的購物車內容都可以儲存在會話狀態中。

您必須在伺服器上有記憶體資源才能保留 ASP 會話狀態,而所需的記憶體數量會根據您儲存在每個會話中的資訊量而有所不同。 為了協助規範 IIS 7 將使用的資源,您可以指定會話狀態的設定。 例如, max 屬性會指定要儲存的會話數目上限,而 timeout 屬性會指定每個 ASP 會話的持續時間。

如果您的應用程式不需要會話狀態,將 allowSessionState 屬性設定為 false 將會停用 ASP 會話狀態。

相容性

版本 備註
IIS 10.0 未在 IIS 10.0 中修改專案 <session>
IIS 8.5 未在 IIS 8.5 中修改專案 <session>
IIS 8.0 未在 IIS 8.0 中修改專案 <session>
IIS 7.5 未在 IIS 7.5 中修改專案 <session>
IIS 7.0 元素 <session><asp> 元素是在 IIS 7.0 中引進。
IIS 6.0 元素 <session> 會取代下列 IIS 6.0 中繼基底屬性:
  • AspAllowSessionState
  • AspKeepSessionIDSecure
  • AspSessionMax
  • AspSessionTimeout

安裝程式

若要在 Web 服務器上支援及設定 ASP 應用程式,您必須安裝 ASP 模組。 若要安裝 ASP 模組,請使用下列步驟。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在工作列上,按一下 [伺服器管理員]
  2. [伺服器管理員] 中,按一下 [管理] 功能表,然後按一下 [新增角色及功能]。
  3. 在 [ 新增角色及功能精 靈] 中,按 [ 下一步]。 選取安裝類型,然後按 [ 下一步]。 選取目的地伺服器,然後按 [ 下一步]。
  4. 在 [ 伺服器角色] 頁面上,依序展開 [ Web 服務器 (IIS) ]、[ Web 服務器]、[ 應用程式開發],然後選取 [ ASP]。
    展開 [Web 服務器和應用程式開發] 窗格的影像,其中已選取 [S P]。
  5. 如果 [ 新增 ASP? 所需的功能? ] 對話方塊隨即出現,請按一下 [ 新增功能]。 (只有在您尚未在 server 上安裝 ISAPI 擴充功能角色服務時,才會顯示此頁面。)
  6. 在 [伺服器角色] 頁面上,按 [下一步]。
  7. 在 [選取功能] 頁面上,按 [下一步]
  8. 在 [確認安裝選項] 頁面上,按一下 [安裝]
  9. 在 [結果] 頁面上,按一下 [關閉]

Windows 8 或 Windows 8.1

  1. 在 [開始] 畫面上,將指標一路移至左下角,以滑鼠右鍵按一下 [開始] 按鈕,然後按一下[主控台]。

  2. 主控台中,按一下 [程式和功能],然後按一下 [開啟或關閉 Windows 功能]。

  3. 依序展開 [Internet Information Services]、[ 萬維網服務]、[ 應用程式開發功能],然後選取 [ASP]。
    [開啟或關閉 Windows 功能] 頁面的螢幕擷取畫面,其中已展開 [應用程式開發功能] 窗格,並已選取 [S P]。

    注意

    如果尚未安裝 ISAPI 擴充功能角色,將會選取它。

  4. 按一下 [確定]。

  5. 按一下 [關閉] 。

Windows Server 2008 或 Windows Server 2008 R2

  1. 在工作列上,按一下 [開始],指向 [系統管理工具],然後按一下[伺服器管理員]。
  2. [伺服器管理員階層] 窗格中,展開 [角色],然後按一下 [Web 服務器] (IIS)
  3. 在 [ Web Server (IIS) ] 窗格中,捲動至 [ 角色服務 ] 區段,然後按一下 [ 新增角色服務]。
  4. 在 [新增角色服務精靈] 的 [選取角色服務] 頁面上,選取[ASP]。
    [新增角色服務精靈] 的 [選取角色服務] 頁面螢幕擷取畫面,其中已選取 S P。
  5. 如果 [ ASP 所需的新增角色服務 ] 對話方塊出現,請按一下 [ 新增必要的角色服務]。 (只有在您尚未在 server 上安裝 ISAPI 擴充功能角色服務時,才會顯示此頁面。)
    [新增角色服務] 對話方塊的螢幕擷取畫面,其中顯示 [新增 S P 問題所需的角色服務] 和 [新增必要角色服務] 按鈕。
  6. 在 [選取角色服務] 頁面上,按 [下一步]
  7. 在 [確認安裝選項] 頁面上,按一下 [安裝]
  8. 在 [結果] 頁面上,按一下 [關閉]

Windows Vista 或 Windows 7

  1. 在工作列上,按一下 [開始],然後按一下[主控台]。
  2. 主控台中,按一下 [程式和功能],然後按一下[開啟或關閉 Windows 功能]。
  3. 依序展開 [Internet Information Services]、[ World Wide Web 服務] 和 [ 應用程式開發功能]。
  4. 選取 [ASP],然後按一下 [ 確定]。
    [開啟或關閉 Windows 功能] 頁面的螢幕擷取畫面,其中顯示 [應用程式開發功能] 窗格已展開,並已選取 [S P]。

作法

如何設定月臺或應用程式的 ASP 會話狀態設定

  1. 開啟 [Internet Information Services (IIS) 管理員

    • 如果您使用 Windows Server 2012 或 Windows Server 2012 R2:

      • 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
    • 如果您使用 Windows 8 或 Windows 8.1:

      • 按住Windows鍵,按字母X,然後按一下[主控台]。
      • 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

      • 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Vista 或 Windows 7:

      • 在工作列上,按一下 [開始],然後按一下[主控台]。
      • 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services] (IIS) Manager
  2. 在 [ 連線] 窗格中,展開伺服器名稱、展開 [網站],然後流覽至您想要設定的網站或 Web 應用程式。

  3. 在月臺或應用程式 [首頁] 窗格中,按兩下 [ASP]。
    pf 應用程式 [首頁] 窗格的螢幕擷取畫面,其中已醒目提示 S P。

  4. [ASP ] 窗格中,展開 [ 會話屬性 ] 區段並設定您想要的設定。
    展開並醒目提示 [會話屬性] 區段的 [S P] 窗格影像。

  5. 在 [動作] 窗格中按一下 [套用]

組態

屬性

屬性 描述
allowSessionState 選擇性的 Boolean 屬性。

指定是否啟用 ASP 應用程式的會話狀態持續性。

預設值是 true
keepSessionIdSecure 選擇性的 Boolean 屬性。

指定是否在透過安全會話通道指派時,將會話識別碼傳送為安全 Cookie。

預設值是 true
max 選擇性 uint 屬性。

指定並行會話的最大數目。

預設值是 4294967295
timeout 選擇性 timeSpan 屬性。

指定在建立與物件相關聯之最後一個要求之後維護會話物件的最長期間 (hh:mm:ss) 。

預設值是 00:20:00

子元素

無。

組態範例

下列組態範例會啟用 ASP 會話狀態、將 ASP 會話數目上限設定為 1000,並將預設網站的會話逾時設定為 10 分鐘。

<location path="Default Web Site">
   <system.webServer>
      <asp>
         <session allowSessionState="true" max="1000" timeout="00:10:00" />
      </asp>
   </system.webServer>
</location>

範例程式碼

下列程式碼範例會啟用 ASP 會話狀態、將 ASP 會話數目上限設定為 1000,並將預設網站的會話逾時設定為 10 分鐘。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/asp /session.allowSessionState:"True" /commit:apphost
appcmd.exe set config "Default Web Site" -section:system.webServer/asp /session.max:"1000" /commit:apphost
appcmd.exe set config "Default Web Site" -section:system.webServer/asp /session.timeout:"00:10:00" /commit:apphost

注意

當您使用AppCmd.exe設定這些設定時,請務必將 認可 參數 apphost 設定為 。 這會將組態設定認可至ApplicationHost.config檔案中適當的位置區段。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection aspSection = config.GetSection("system.webServer/asp", "Default Web Site");

         ConfigurationElement sessionElement = aspSection.GetChildElement("session");
         sessionElement["allowSessionState"] = true;
         sessionElement["max"] = 1000;
         sessionElement["timeout"] = TimeSpan.Parse("00:10:00");

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim aspSection As ConfigurationSection = config.GetSection("system.webServer/asp", "Default Web Site")

      Dim sessionElement As ConfigurationElement = aspSection.GetChildElement("session")
      sessionElement("allowSessionState") = True
      sessionElement("max") = 1000
      sessionElement("timeout") = TimeSpan.Parse("00:10:00")

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var aspSection = adminManager.GetAdminSection("system.webServer/asp", "MACHINE/WEBROOT/APPHOST/Default Web Site");

var sessionElement = aspSection.ChildElements.Item("session");
sessionElement.Properties.Item("allowSessionState").Value = true;
sessionElement.Properties.Item("max").Value = 1000;
sessionElement.Properties.Item("timeout").Value = "00:10:00";

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set aspSection = adminManager.GetAdminSection("system.webServer/asp", "MACHINE/WEBROOT/APPHOST/Default Web Site")

Set sessionElement = aspSection.ChildElements.Item("session")
sessionElement.Properties.Item("allowSessionState").Value = True
sessionElement.Properties.Item("max").Value = 1000
sessionElement.Properties.Item("timeout").Value = "00:10:00"

adminManager.CommitChanges()