简体   繁体   English

如果用户已经登录,请重定向到其他页面

[英]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. 我试图让我的网站按Twitter.com的方式工作。 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. 如果您尚未登录www.twitter.com,则打开的第一页是允许访客登录/注册的主页。 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. 但是,如果这不是您第一次访问该网站,则您将保持登录状态,而下次访问www.twitter.com时,加载的第一页是时间轴。

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" LoggedIn下面有一个红色错误行,上面写着“要评估的表达式”

How could I correct this, so if a user is already logged in, the first page that loads is abc.aspx 我该如何纠正,所以如果用户已经登录,则加载的第一页是abc.aspx。

Try checking the HttpContext instead. 尝试改为检查HttpContext。

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. 您可以在上面的代码中添加此代码,以确保在实际登录时它们进入abc.aspx。

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 有关登录控件的更多信息http://forums.asp.net/t/1403132.aspx/1

If you are using Forms Authentication just use defaultUrl="abc.aspx" . 如果您使用的是Forms Authentication只需使用defaultUrl="abc.aspx"

web.config example web.config示例

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

See forms Element for authentication (ASP.NET Settings Schema) for details. 有关详细信息,请参见表单用于身份验证的元素(ASP.NET设置架构)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户已经登录 Blazor 服务器,如何将身份登录页面设置为启动页面并将用户重定向到仪表板 - How To Make Identity Login Page As Startup Page And Redirect User to Dashboard If User Is Already Logged-in In Blazor Server 用户已经登录后,如何重定向到未授权页面而不是登录页面? - How do I redirect to a not-authorized page instead of the login page when the user is already logged in? 在已登录的 session 上使用不同用户登录 OIDC - OIDC login with different user on an already logged in session 如果只有登录用户,如何重定向或访问页面 - How to redirect or visit page if only there is a logged in user 以编程方式登录网站并将用户重定向到登录页面? - Programmatically login to a website and redirect the user to the logged in page? 已登录身份后重定向到登录页面 .net 6 - Redirect to login page after being already logged in identity .net 6 检查用户是否已经登录 - Check if user is already logged in 用户未登录时(asp)重定向到登录页面 - Redirect to login-page when user is not logged in (asp) 如何检查用户是否已登录? - How to check if user is already logged in? 如何将已登录的用户重定向到 Asp.Net Core web 应用程序中的索引(剃须刀页面) - How can I redirect an already logged user to Index in Asp .Net Core web Application (razor pages)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM