Click here to Skip to main content
13,355,018 members (56,495 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 13 Apr 2010

Coupling ASP.NET Session State With Forms Authentication

, 15 Apr 2010
Rate this:
Please Sign up or sign in to vote.
Coupling ASP.NET Session State With Forms Authentication
free hit counters

Today I was talking with João about a way to couple the lifetime of the ASP.NET session state with the lifetime of Forms Authentication ticket.

My idea was to store the session ID in the UserData property of the forms authentication ticket upon logon and retrieve it with a custom session ID manager.

The login code would be something like this:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    bool isPersistent = this.Login1.RememberMeSet;
    string username = this.Login1.UserName;
    var ticket = new FormsAuthenticationTicket(

    // Encrypt the ticket.
    var encryptedTicket = FormsAuthentication.Encrypt(ticket);

    // Create the cookie.
    this.Response.Cookies.Add(new HttpCookie
	(FormsAuthentication.FormsCookieName, encryptedTicket));

    // Redirect back to original URL.
    this.Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent));

For the purpose of this test, I am using a Guid as the session ID.

The session ID manager will return this session ID when queried by the session state HTTP module:

public class SessionIdManager : global::System.Web.SessionState.ISessionIDManager
    #region ISessionIDManager Members

    public string CreateSessionID(HttpContext context)
        return GetDummySessionIdOrRedirectToLoginPage(context);

    public string GetSessionID(HttpContext context)
        return GetSessionIdFromFormsIdentity(context);

    public void Initialize()

    public bool InitializeRequest(HttpContext context, 
	bool suppressAutoDetectRedirect, out bool supportSessionIDReissue)
        supportSessionIDReissue = false;
        return GetSessionIdFromFormsIdentity(context) == null;

    public void RemoveSessionID(HttpContext context)

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

    public bool Validate(string id)
        return true;


    private static string GetSessionIdFromFormsIdentity(HttpContext context)
        var identity = context.User != null ? 
		context.User.Identity as FormsIdentity : null;

        if ((identity == null) || (identity.Ticket == null) || 
            return GetDummySessionIdOrRedirectToLoginPage(context);
            return identity.Ticket.UserData;

    private static string GetDummySessionIdOrRedirectToLoginPage(HttpContext context)
        if (context.Request.CurrentExecutionFilePath.Equals
	(FormsAuthentication.DefaultUrl, StringComparison.OrdinalIgnoreCase)
           || context.Request.CurrentExecutionFilePath.Equals
		(FormsAuthentication.LoginUrl, StringComparison.OrdinalIgnoreCase))
            return Guid.NewGuid().ToString("N");
            return null;

NOTE: Although this might work, it’s just an intellectual exercise and wasn’t fully tested.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paulo Morgado
Software Developer (Senior) Paulo Morgado
Portugal Portugal

You may also be interested in...

Comments and Discussions

Generala not so subtle problem with this idea.. Pin
Sky Sanders15-Apr-10 16:24
memberSky Sanders15-Apr-10 16:24 
GeneralRe: a not so subtle problem with this idea.. Pin
Paulo Morgado15-Apr-10 16:38
memberPaulo Morgado15-Apr-10 16:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 15 Apr 2010
Article Copyright 2010 by Paulo Morgado
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid