简体   繁体   中英

If user is already logged in, redirect to a different page

I am trying to get my website to work the way Twitter.com works. If you are not logged in, www.twitter.com, first page that opens up is the homepage which allows the guest to log in/register. However, if this is not the first time you have visted the website, you remain logged in, and the next time you go to www.twitter.com, the first page that loads is the timeline.

From the homepage page load I tried the following code but I'm pretty sure it's not right

protected void Page_Load(object sender, EventArgs e)
{
    if (Login1.LoggedIn == true)
    {
        Response.Redirect("abc.aspx");
    }    
}

There's a red error line underneath LoggedIn saying "Expression to evaluate"

How could I correct this, so if a user is already logged in, the first page that loads is abc.aspx

Try checking the HttpContext instead.

protected void Page_Load(object sender, EventArgs e)
{
    if (User.Identity.IsAuthenticated)
    {
        Response.Redirect("abc.aspx");
    }    
}

And you could add this with the code above to make sure they go to abc.aspx when the actual login happens.

protected void Login1_LoggedIn(object sender, EventArgs e)
{
     Response.Redirect("abc.aspx");
}

More info about the login control http://forums.asp.net/t/1403132.aspx/1

If you are using Forms Authentication just use defaultUrl="abc.aspx" .

web.config example

<authentication mode="Forms">
    <forms loginUrl="Login.aspx" defaultUrl="abc.aspx" />
</authentication>

See forms Element for authentication (ASP.NET Settings Schema) for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM