如果用户有身份验证 cookie,则重定向到默认页面

发布于 2024-12-08 14:10:19 字数 407 浏览 9 评论 0原文

我的asp.net登录页面中有以下代码:

if (Request.QueryString["ReturnUrl"] != null)
        FormsAuthentication.RedirectFromLoginPage(UserLogin.UserName, UserLogin.RememberMeSet);
    else
        FormsAuthentication.SetAuthCookie(UserLogin.UserName, UserLogin.RememberMeSet);

我想要的场景是:

当用户进入登录页面时,将检查他是否有身份验证cookie,如果有,则自动重定向到默认页面(这是一个只有经过身份验证的用户才能看到的页面)。

如何才能实现这一目标?

I have the following code in 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2024-12-15 14:10:19

例如,将其放在 Page_Init 中...

  if (Request.IsAuthenticated) {
            Response.Redirect(Request.QueryString["ReturnUrl"]);
  }

如果用户登录,它只会将用户弹回目的地。

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.

东风软 2024-12-15 14:10:19

如果身份验证 cookie 存在并且有效,则上下文将填充用户数据。只需检查是否:

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 );
      }
   }

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 );
      }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文