session – Holding userid in ASP.net – Education Career Blog

I am using ASP.net for developing an intranet website. I need to hold the userid across all postbacks for all the pages in the website. Is it advisable to hold those information in Session or somether way is available.

,

FormsAuthentication is also capable of holding a custom userid, and solves a lot of things for you like setting the cookie, login page redirection etc.

You can set the userid using the RedirectFromLoginPage method and then use the FormsAuthentication_OnAuthenticate event to find and set the Page.User property, to access all the other logic you need.

,

If using Forms Authentication, then the username will be available as:

Page.User.Identity.Username

If you need to hold another piece of information alongisde this, then use Session.

PS. I recommend using a class to wrap your session variables for strong-typedness and potential default values.

,

The most common ways of doing this are using either the ASP.NET Session Object or using cookies.

Either way will work well, but if you wish for their user information to persist even after the session has timed out (such as the closing of a browser window), then you would want to look into cookies. Session information will be disposed upon closing of a browser, or the activity timeout has been reached.

Leave a Comment