2

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

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

我想要的场景是:

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

如何实现?

4

2 回答 2

3

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

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

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

于 2011-10-08T19:12:17.507 回答
3

如果身份验证 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 );
      }
   }
于 2011-10-08T19:13:31.647 回答