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

検証するセッション識別子。

戻り値

Boolean

セッション識別子が有効な場合は true。それ以外の場合は false

実装

次のコード例は、クラスを継承SessionIDManagerし、メソッドを指定して検証するメソッドでメソッドValidateをオーバーライドCreateSessionIDするクラスをGuidSessionID示しています。

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 文字列が、a ~ z の小文字と 0 から 5 の数字で構成される 24 文字の文字列であり、セッション ID の最大長が 80 文字を超えていないことを確認します。

このメソッドは GetSessionID 、HTTP 要求からセッション識別子を取得するときにメソッドを呼び出 Validate して、指定されたセッション識別子が適切に書式設定されていることを確認します。

注意 (継承者)

セッション状態 ASP.NET 使用するカスタム セッション識別子を指定するには、そのクラスを継承SessionIDManagerするクラスを作成し、独自のカスタム実装で and Validate(String) メソッドをオーバーライドCreateSessionID(HttpContext)します。 カスタム セッション識別子を作成する場合でも、セッション ID はクラスによって 80 文字に SessionIDManager 制限されます。

適用対象

こちらもご覧ください