33

The default cookie name for the Session Id in ASP.NET is ASP.NET_SessionId. It's also possible to change this name to something else like <sessionState cookieName="FooBar" />.

Is there a member to easily access this name like with FormsAuthentication.FormsCookieName?

TheCloudlessSky
  • 18,608
  • 15
  • 75
  • 116

1 Answers1

68

Taking into account that you say that you set an different name for the cookie on the web.config then I'd say you could read the cookie name from there

SessionStateSection sessionStateSection =
  (System.Web.Configuration.SessionStateSection)
  ConfigurationManager.GetSection("system.web/sessionState");

string cookieName =  sessionStateSection.CookieName;
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • 1
    Ok but what about the case where it *doesn't* get changed? Will this return the default value of `ASP.NET_SessionId` as well? – TheCloudlessSky Sep 17 '10 at 23:31
  • 6
    Yes, when you didn't change it you get "ASP.NET_SessionId" – Claudio Redi Sep 18 '10 at 01:20
  • 1
    Aren't there really no more straightforward ways? Has Microsoft really not think of a class to help with that? – Serge Profafilecebook Dec 10 '14 at 15:27
  • 1
    @SergeProfafilecebook - It seems very straightforward and easy to work with to me. – Peter Ivan Aug 16 '17 at 13:20
  • `CookieName` will be defaulted to `"ASP.NET_SessionId"` if nont is specified in the config as it has a `ConfigurationPropertyAttribute` of `[ConfigurationPropertyAttribute("cookieName", DefaultValue = "ASP.NET_SessionId")]` so there is no need to do your own setting/checking – Kevin Smith Nov 29 '17 at 11:43