Share via


SessionIDManager.Validate(String) 方法

定義

取得值,指出工作階段識別項是否有效。

public:
 virtual bool Validate(System::String ^ id);
public virtual bool Validate (string id);
abstract member Validate : string -> bool
override this.Validate : string -> bool
Public Overridable Function Validate (id As String) As Boolean

參數

id
String

要驗證的工作階段識別項。

傳回

如果工作階段識別項有效,則為 true,否則為 false

實作

範例

下列程式碼範例示範繼承 類別的 SessionIDManager 類別,並使用提供 和 驗證 Guid 的方法覆寫 CreateSessionIDValidate 方法。 SessionID

using System;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.SessionState;

namespace Samples.AspNet.Session
{

  public class GuidSessionIDManager : SessionIDManager
  {

    public override string CreateSessionID(HttpContext context)
    {
      return Guid.NewGuid().ToString();
    }

    public override bool Validate(string id)
    {
      try
      {
        Guid testGuid = new Guid(id);

        if (id == testGuid.ToString())
          return true;
      }
      catch
      {
      }

      return false;
    }
  }
}
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Web
Imports System.Web.SessionState


Namespace Samples.AspNet.Session

  Public Class GuidSessionIDManager
    Inherits SessionIDManager

    Public Overrides Function CreateSessionID(context As HttpContext) As String
      Return Guid.NewGuid().ToString()
    End Function

    Public Overrides Function Validate(id As String) As Boolean
      Try
        Dim testGuid As Guid = New Guid(id)

        If id = testGuid.ToString() Then _
          Return True
      Catch
      
      End Try

      Return False
    End Function

  End Class

End Namespace

若要使用此範例中示範的自訂類別,請將 Web.config 檔案中的 HTTP 模組取代 SessionID 為您的自訂類別,如下列範例所示。

<httpModules>
  <remove name="SessionID" />
  <add name="SessionID"
       type="Samples.AspNet.Session.GuidSessionIDManager" />
</httpModules>

備註

此方法不適合從應用程式程式碼呼叫。

方法 Validate 會驗證提供的 id 是 24 個字元字串,其中包含從 到 z 的小寫字元,以及從 0 到 5 的數位,且會話識別碼的最大長度不超過 80 個字元。

方法 GetSessionID 會從 HTTP 要求擷取會話識別碼時呼叫 Validate 方法,以確保提供的會話識別碼格式正確。

給繼承者的注意事項

您可以藉由建立繼承 SessionIDManager 類別的類別,並使用您自己的自訂實作覆 CreateSessionID(HttpContext) 寫 和 Validate(String) 方法,提供自訂會話識別碼供 ASP.NET 會話狀態使用。 即使您建立自訂會話識別碼,會話識別碼仍受限於 SessionIDManager 類別的 80 個字元。

適用於

另請參閱