【问题标题】:Redirect to default page if the user has authentication cookie如果用户有身份验证 cookie,则重定向到默认页面
【发布时间】:2011-12-03 16:31:50
【问题描述】:

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

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

我想要的场景是:

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

如何做到这一点?

【问题讨论】:

    标签: asp.net webforms forms-authentication remember-me


    【解决方案1】:

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

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

    如果用户已登录,它只会将用户跳转到目的地。

    【讨论】:

      【解决方案2】:

      如果身份验证 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 );
            }
         }
      

      【讨论】:

      • 它是 FormsAuthentication.DefaultUrl,不是吗?
      • 谢谢,我记错了:)
      猜你喜欢
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2021-06-19
      • 1970-01-01
      相关资源
      最近更新 更多