Search code examples
webformsasp.net-identitypassword-protection

protect all pages does not work in asp.net identity for webforms


I have created a new web application using web forms and individual account, I can create a user and login. But I need all pages to be password protected. What do I need to add in order for that to work?


Solution

  • You didn't added a lot of details, but I think the solution for you would be to check if the user is authenticated at Page_Load.

    This should be included in your base Page class (assuming you have one and your not directly implementing the Page class).

     public abstract class MyBaseAuthProtectedPage : Page
     {
        private void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if(!User.Identity.IsAuthenticated)
                {
                     //not authenticated logic here
                }
            }
         }
    ....
     }
    

    All your pages should implement the MyBaseAuthProtectedPage class.