Share via


ISessionIDManager 인터페이스

정의

사용자 지정 세션 상태 식별자 관리자가 구현해야 하는 계약을 정의합니다.

public interface class ISessionIDManager
public interface ISessionIDManager
type ISessionIDManager = interface
Public Interface ISessionIDManager
파생

예제

다음 코드 예제에서는 관리자는 쿠키 기반 세션 ID를 구현 하는 클래스를 보여 줍니다.

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

namespace Samples.AspNet.Session
{

  public class MySessionIDManager : IHttpModule, ISessionIDManager
  {

    private SessionStateSection pConfig = null;

    //
    // IHttpModule Members
    //

    //
    // IHttpModule.Init
    //

    public void Init(HttpApplication app)
    {
      // Obtain session-state configuration settings.

      if (pConfig == null)
      {
        Configuration cfg =
          WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
        pConfig = (SessionStateSection)cfg.GetSection("system.web/sessionState");
      }
    }

    //
    // IHttpModule.Dispose
    //

    public void Dispose()
    {
    }

    //
    // ISessionIDManager Members
    //

    //
    // ISessionIDManager.Initialize
    //

    public void Initialize()
    {
    }

    //
    // ISessionIDManager.InitializeRequest
    //

    public bool InitializeRequest(HttpContext context, 
                                  bool suppressAutoDetectRedirect, 
                                  out bool supportSessionIDReissue)
    {
      if (pConfig.Cookieless == HttpCookieMode.UseCookies)
      {
        supportSessionIDReissue = false;
        return false;
      }
      else
      {
        supportSessionIDReissue = true;
        return context.Response.IsRequestBeingRedirected;
      }
    }

    //
    // ISessionIDManager.GetSessionID
    //
    public string GetSessionID(HttpContext context)
    {
      string id = null;

      if (pConfig.Cookieless == HttpCookieMode.UseUri)
      {
        // Retrieve the SessionID from the URI.
      }
      else
      {
        id = context.Request.Cookies[pConfig.CookieName].Value;
      }      

      // Verify that the retrieved SessionID is valid. If not, return null.

      if (!Validate(id))
        id = null;

      return id;
    }

    //
    // ISessionIDManager.CreateSessionID
    //

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

    //
    // ISessionIDManager.RemoveSessionID
    //

    public void RemoveSessionID(HttpContext context)
    {
      context.Response.Cookies.Remove(pConfig.CookieName);
    }

    //
    // ISessionIDManager.SaveSessionID
    //

    public void SaveSessionID(HttpContext context, string id, out bool redirected, out bool cookieAdded)
    {
      redirected = false;
      cookieAdded = false;

      if (pConfig.Cookieless == HttpCookieMode.UseUri)
      {
        // Add the SessionID to the URI. Set the redirected variable as appropriate.

        redirected = true;
        return;
      }
      else
      {
        context.Response.Cookies.Add(new HttpCookie(pConfig.CookieName, id));
        cookieAdded = true;
      }
    }

    //
    // ISessionIDManager.Validate
    //

    public 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 MySessionIDManager
    Implements IHttpModule, ISessionIDManager

    Private pConfig As SessionStateSection = Nothing

    '
    ' IHttpModule Members
    '

    '
    ' IHttpModule.Init
    '

    Public Sub Init(app As HttpApplication) Implements IHttpModule.Init
    
      ' Obtain session-state configuration settings.

      If pConfig Is Nothing Then      
        Dim cfg As System.Configuration.Configuration = _
          WebConfigurationManager.OpenWebConfiguration( _
            System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath)
        pConfig = CType(cfg.GetSection("system.web/sessionState"), SessionStateSection)
      End If

    End Sub


    '
    ' IHttpModule.Dispose
    '

    Public Sub Dispose() Implements IHttpModule.Dispose
    
    End Sub



    '
    ' ISessionIDManager.Initialize
    '

    Public Sub Initialize() Implements ISessionIDManager.Initialize

    End Sub


    '
    ' ISessionIDManager.InitializeRequest
    '

    Public Function InitializeRequest(context As HttpContext,  _
                                      suppressAutoDetectRedirect As Boolean,  _
                                      ByRef supportSessionIDReissue As Boolean) As Boolean _
                                      Implements ISessionIDManager.InitializeRequest

      If pConfig.Cookieless = HttpCookieMode.UseCookies Then
        supportSessionIDReissue = False
        Return False
      Else
        supportSessionIDReissue = True
        Return context.Response.IsRequestBeingRedirected
      End If
    End Function



    '
    ' ISessionIDManager Members
    '


    '
    ' ISessionIDManager.GetSessionID
    '
    Public Function GetSessionID(context As HttpContext) As String _
      Implements ISessionIDManager.GetSessionID
    
      Dim id As String = Nothing

      If pConfig.Cookieless = HttpCookieMode.UseUri Then
        ' Retrieve the SessionID from the URI.
      Else
        id = context.Request.Cookies(pConfig.CookieName).Value
      End If    

      ' Verify that the retrieved SessionID is valid. If not, return Nothing.

      If Not Validate(id) Then _
        id = Nothing

      Return id
    End Function

    '
    ' ISessionIDManager.CreateSessionID
    '

    Public Function CreateSessionID(context As HttpContext) As String _
      Implements ISessionIDManager.CreateSessionID
    
      Return Guid.NewGuid().ToString()
    End Function

    '
    ' ISessionIDManager.RemoveSessionID
    '

    Public Sub RemoveSessionID(context As HttpContext) _
      Implements ISessionIDManager.RemoveSessionID

      context.Response.Cookies.Remove(pConfig.CookieName)

    End Sub


    '
    ' ISessionIDManager.SaveSessionID
    '

    Public Sub SaveSessionID(context As HttpContext, _
                             id As String, _
                             ByRef redirected As Boolean, _
                             ByRef cookieAdded As Boolean) _
      Implements ISessionIDManager.SaveSessionID
    
      redirected = False
      cookieAdded = False

      If pConfig.Cookieless = HttpCookieMode.UseUri Then

        ' Add the SessionID to the URI. Set the redirected variable as appropriate.

        redirected = True
        Return
      Else
        context.Response.Cookies.Add(New HttpCookie(pConfig.CookieName, id))
        cookieAdded = True
      End If
    End Sub


    '
    ' ISessionIDManager.Validate
    '

    Public Function Validate(id As String) As Boolean _
      Implements ISessionIDManager.Validate
    
      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

설명

ISessionIDManager 세션 식별자 값에 대 한 사용자 지정 관리자를 만들기 위해 구현 해야 하는 메서드를 식별 하는 인터페이스입니다. ISessionIDManager 인터페이스 구현은 세션 식별자 값을 생성하고 유효성을 검사하며, HTTP 요청에서 세션 식별자 값을 검색할 뿐만 아니라 HTTP 응답에서 세션 식별자의 스토리지를 관리합니다. 사용 하 여 사용자 지정 세션 ID 관리자를 사용 하도록 설정 하면 합니다 sessionIDManagerType 특성을 sessionState 요소 (ASP.NET 설정 스키마) 구성 요소.

경우에 ISessionIDManager 인터페이스 구현에서 쿠키 없는 세션 식별자를 지원 합니다, 전송 및 ISAPI 필터와 같은 URL에서 세션 식별자를 검색에 대 한 솔루션을 구현 해야 합니다.

ASP.NET 세션 상태에서 사용할 사용자 지정 세션 식별자 값을 제공 하려는 경우 상속 된 클래스를 만들 수 있습니다 합니다 SessionIDManager 클래스 및만 재정의 합니다 CreateSessionIDValidate 사용자 고유의 사용자 지정 구현으로 메서드. 기본에 의존 하는 동안 고유한 세션 식별자 값을 제공할 수 있습니다이 SessionIDManager 클래스 HTTP 응답에는 값을 저장 하 고 HTTP 요청에서 값을 검색 합니다. 재정의 하는 예는 SessionIDManager 클래스 및 이러한 메서드를 구현에 대해 제공 된 예제를 참조 하세요 합니다 CreateSessionID 메서드를 SessionIDManager 클래스.

메서드

CreateSessionID(HttpContext)

고유한 세션 식별자를 만듭니다.

GetSessionID(HttpContext)

현재 HTTP 요청의 컨텍스트에서 세션 식별자를 가져옵니다.

Initialize()

초기화는 SessionIDManager 개체입니다.

InitializeRequest(HttpContext, Boolean, Boolean)

SessionIDManager 개체의 요청별 초기화를 수행합니다.

RemoveSessionID(HttpContext)

쿠키나 URL에서 세션 식별자를 삭제합니다.

SaveSessionID(HttpContext, String, Boolean, Boolean)

새로 만든 세션 식별자를 HTTP 응답에 저장합니다.

Validate(String)

제공된 세션 식별자가 유효한지 확인합니다.

적용 대상

추가 정보