ISessionIDManager.GetSessionID(HttpContext) Metod

Definition

Hämtar sessionsidentifieraren från kontexten för den aktuella HTTP-begäran.

public:
 System::String ^ GetSessionID(System::Web::HttpContext ^ context);
public string GetSessionID(System.Web.HttpContext context);
abstract member GetSessionID : System.Web.HttpContext -> string
Public Function GetSessionID (context As HttpContext) As String

Parametrar

context
HttpContext

Det aktuella HttpContext objektet som refererar till serverobjekt som används för att bearbeta HTTP-begäranden (till exempel Request egenskaperna och Response ).

Returer

Den aktuella sessionsidentifieraren som skickas med HTTP-begäran.

Exempel

I följande kodexempel visas en delvis implementerad GetSessionID metod. Om din anpassade sessions-ID-hanterare stöder cookielösa sessionsidentifierare måste du implementera en lösning för att skicka och hämta sessionsidentifierare i URL:en, till exempel ett ISAPI-filter.

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;
}
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

Kommentarer

Metoden GetSessionID anropas av SessionStateModule under HttpApplication.AcquireRequestState - och-händelserna HttpApplication.EndRequest . Om du inte kan hämta en giltig sessionsidentifierare från HTTP-begäran returnerar du null. Om mottagningarna SessionStateModule från null metoden anropas GetSessionIDCreateSessionID metoden för att hämta en ny sessionsidentifierare för en ny session.

Om det är möjligt att värdet som returneras av implementeringen CreateSessionID innehåller tecken som inte är giltiga i ett HTTP-svar eller en http-begäran bör du använda UrlEncode metoden för att koda värdet sessionsidentifierare i SaveSessionID metodimplementeringen och UrlDecode metoden för att avkoda värdet för sessionsidentifierare i metodimplementeringen GetSessionID .

Gäller för

Se även