Redirect to default page if the user has authentication cookie
I have the following code i开发者_如何学运维n my asp.net login page:
if (Request.QueryString["ReturnUrl"] != null)
FormsAuthentication.RedirectFromLoginPage(UserLogin.UserName, UserLogin.RememberMeSet);
else
FormsAuthentication.SetAuthCookie(UserLogin.UserName, UserLogin.RememberMeSet);
The scenario I want is:
when the user enters the login page, it will be checked if he has an authentication cookie, and if so, he is automatically redirected to the default page (which is a page that only authenticated users can see).
How can this be achieved ?
Place this in the Page_Init for example...
if (Request.IsAuthenticated) {
Response.Redirect(Request.QueryString["ReturnUrl"]);
}
It'll just bounce the user onwards to the destination if they're logged in.
If the authentication cookie is present and it is valid, the context will be populated with the user data. Just check then if:
public class Login_Page {
public void Page_Load( ... ) {
if ( this.Context.User != null && this.Context.User.Identity != null &&
this.Context.User.Identity.IsAuthenticated )
this.Response.Redirect( FormsAuthentication.DefaultUrl );
}
}
精彩评论