Search code examples
c#asp.net-mvcsimplemembership

How to create a session number from the ASPXAUTH cookie?


I would like to make a human readable number from the cookie value to be used as a session number. It should be unique relative to the user id.

I know how to retrieve the cookie, I would be interested in an algorithm to achieve this or maybe another idea to generate a session number?

What about:

String.Format("{0}{1}", userId, cookieValue.GetHashCode());

Solution

  • You can get a SessionID in your controller using this:

    Session.SessionID
    

    It will contain the actual SessionID which will look like this: q42eezgj3l5msxpwr5mbvhex

    According to msdn it is stored in the cookie:

    The SessionID property is used to uniquely identify a browser with session data on the server. The SessionID value is randomly generated by ASP.NET and stored in a non-expiring session cookie in the browser. The SessionID value is then sent in a cookie with each request to the ASP.NET application.

    The ASPXAUTH cookie is used to determine if the user is logged in. If you need to decrypt it you should do the following:

    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];//.ASPXAUTH
    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);