2011-10-08 32 views
2

我在我的asp.net登錄頁面下面的代碼:重定向到默認頁面,如果用戶身份驗證cookie

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

我想要的場景是:

當用戶進入登錄頁面,會檢查他是否有認證cookie,如果有,他會自動重定向到默認頁面(該頁面只有經過認證的用戶才能看到)。

這是如何實現的?

回答

3

將這個在Page_Init例如...

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

這將只是反彈的用戶開始到目的地,如果他們已經登錄。

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

它是FormsAuthentication.DefaultUrl,不是嗎? – user560498

+0

謝謝,我的記憶失敗了:) –

相關問題