Best way to manage Session state and Authentication

xprogrammerx 20 Reputation points
2023-10-09T22:46:40.85+00:00

Greetings everyone,

I'm developing an ASP.NET MVC App with Forms authenticatioin and the Membership (API) (i can't use Identity for now).

I noticed that the Session state is not always in sync with the authentication session , meaning, that a user can have an expired session and still be logged in, which will result in exception when trying to retrieve his authentication informations from the session.

Here is what i observed :

-Session is mapped to the server by an identifier set in a cookie .

-if the sessionState timeout expires than the session expires

-If we close the browser (all the browser tabs) than the cookie is deleted , and still the session lives on the server ,but because it can't be mapped to the session by the cookie , it creates a new session thus reinitializing it.

-if we stop the app on the server or the server recycles the session is lost

-The authentication identifier is also stored in a cookie.

-If the forms timeout expires than the user will be logged off.

-The authentication cookie is also removed when the browser is closed (all the browser tabs) therefore user is logged out ,Unless FormsAuthentication.createPersistentCookie is true than the user is still logged in even if he closes the browser.

-However if the user does not close the browser and the app in the server is stopped or the server recycles , than the user is still kept logged in, because the authentication cookie is still in the browser, and yet the session is lost.

I was wandering what is the best way to manage session state with forms authentication , i read that the session should not be synchronized with the authentication

process because they are not the same thing.

I was thinking to fix the issue in bold above, i would implement Session_Start in globall.asax to retrieve informations about the user if he is authenticated each time the session starts.

Thank you.

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. SurferOnWww 4,721 Reputation points
    2023-10-10T00:28:53.4966667+00:00

    I was wandering what is the best way to manage session state with forms authentication , i read that the session should not be synchronized with the authentication

    The Session State and Forms Authentication of the ASP.NET are independent. There is no relation between them at all. Therefore, you may forget about the Session State when you consider the Forms Authentication.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,086 Reputation points Volunteer Moderator
    2023-10-10T15:25:56.8433333+00:00

    Both session and authentication use cookies, and the default is browser session only (not stored) and similar timeout. But as stated above, they are independent of each other. You can have have a 90 day persistent session cookie, and 20 minute identity cookie or the reverse.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.