User authentication on page load

I have an application and the user must be logged in before they can access the pages. now, as soon as the user logs in, I store the user's data in a session variable (eg Session ["CurrentUser"]).

now if the user tries to go to the page directly, i check if the Session ["CurrentUser"] value is or not ... if not then the user will be redirected to the login page ...

My problem is that I have done this, or rather have written this "validation code" in almost all of the pages.

I want this code to stay in a specific place and I will just refer to this method all the time in all pages ... now where should I write this method?

thanks.

+2


a source to share


3 answers


You can create a class that inherits from System.Web.UI.Page and then inherit all of your individual page classes. Have you looked at the built-in ASP.net forms authentication methods?



+1


a source


You should take a look at ASP.NET Authentication . This will allow you to secure a section of your website or individual pages through the web.config file and use a cookie for authentication instead of checking the session variable.



+1


a source


You can put it in a base class that extends Page

, and then all your codebase extends that.

Better solution would be to use

Application_AuthenticateRequest

      

pseudo event in Global.asax. You shouldn't use session either, have you looked at Form Authentication?

0


a source







All Articles