What is the best way to redirect a user to the login page?

If I wanted to redirect the user to the login page for some reason (either they are not logged in or we sign them out automatically) what would be the best way to redirect them there? I can manually do a Redirect but I am sure there is probably a more integrated method to do this through ASP.Net membership or something. Also, the Web.config already has the login page, so from a maintainability perspective I don’t want to keep the login URL in many places. Any thoughts?


One of the most consistant methods would be the following:

        /// <summary>

        /// Check if current user is valid, else redirect to the login page

        /// </summary>

        private void CheckUserIsAuthenticated()

        {

            MembershipUser user = Membership.GetUser();

            if (user == null)

                FormsAuthentication.RedirectToLoginPage();

            else

            {

                if (String.IsNullOrEmpty(user.ToString()))

                {

                    FormsAuthentication.RedirectToLoginPage();

                }

            }

        }

By using the RedirectToLoginPage you use the settings in FormsAuthentication (obtained from the Web.Config). Just keep in mind that the current thread continues to execute so any code after the “FormsAuthentication.RedirectToLoginPage();” will in fact execute.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.