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.
Love
Can we use Let's Encrypt, the free and open certificate authority?
Hola! gracias por la info, me sirvió el comando sacandole el nombre del server. En mi caso, fue una migración…
Yes 3rd option helped me too. I removed the WC key Values from config file then started working.
I know this is from 2014. But really, thank you!