2014-09-13 97 views
1

請注意這裏的母版頁編號網絡使用登錄控制和LoginView成功登錄後,頁面重定向

<asp:LoginView ID="LoginView1" runat="server" ViewStateMode="Disabled"> 
       <AnonymousTemplate> 
        <ul> 
         <li><a id="registerLink" runat="server" href="~/LogReg.aspx">Register</a></li> 
         <li><a id="loginLink" runat="server" href="~/LogReg.aspx">Log in</a></li> 
        </ul> 
       </AnonymousTemplate> 
       <LoggedInTemplate> 
        <ul> 
         <li><a id="manage" runat="server" class="username" href="~/Account/Manage.aspx" title="Manage your account"> 
          <asp:LoginName ID="LoginName2" runat="server" CssClass="username" /> 
         </a> 
         </li> 
         <li><a id="A2" runat="server" href="~/Logout.aspx">Log Out</a></li> 
        </ul> 
       </LoggedInTemplate> 
      </asp:LoginView> 

這是登錄頁面代碼

<asp:Login ID="Login1" runat="server" Height="198px" Width="297px" DisplayRememberMe="False" OnAuthenticate="Login1_Authenticate" UserNameLabelText="Email:"> 
      <InstructionTextStyle Font-Italic="True" ForeColor="Black" /> 
      <LoginButtonStyle Font-Size="Small" /> 
      <TextBoxStyle Font-Size="0.8em" CssClass="float-right" Width="180px" Height="12px" /> 
      <LabelStyle CssClass="float-left" /> 
      <TitleTextStyle CssClass="task-title" /> 
     </asp:Login> 

登錄控制的認證事件看起來像:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) 
    { 
     string un = Login1.UserName; 
     string pw = Login1.Password; 

     var c = db.customers.Where(s => s.customer_email == un && s.customer_password == pw && s.customer_deleted != true); 
     if (c.Count() == 1) 
     { 
      e.Authenticated = true; 
      //Something to redirect //Response.Redirect(Page.PreviousPage.ToString()); 
     } 
     else 
      e.Authenticated = false; 

    } 

問題是,當我在這裏使用Response.Redirect函數時,它成功重定向,但loginView模板返回到anonymousTemplate。如果我不使用重定向,模板將更改爲LoggedInTemplate,但我想將其重定向到上一頁。我使用了destinationPageUrl屬性,但它的行爲是一樣的。感謝您的幫助:)

回答

1

問題是在調用Authenticate事件後,Login控件需要創建FormAuthentication cookie。

如果要將已通過身份驗證的用戶重定向到頁面,可以使用LoggedIn事件。

protected void Login1_LoggedIn(object sender, EventArgs e) 
{ 
    // User is authenticated, so do something here. 
} 

通常情況下,我們通過RETURNURL在查詢字符串的登錄頁面。如果登錄控制參見ReturnUrl,它將重定向回該Url。

例如,http://www.yoursite.com/login.aspx?ReturnUrl=%2fAbout%2f

+0

太棒了!謝謝:) – 2014-09-13 15:46:19

+0

Page.PreviousPage在這個事件中顯示爲null,你能不能請告訴我應該採取什麼措施重定向到上一頁,而不是使用會話? 謝謝,現在明白了。 – 2014-09-13 15:56:15

+1

您可以嘗試** Request.UrlReferrer **。但是,我不推薦任何一個,因爲它們不可靠。相反,我們通常在QueryString **中使用** ReturnUrl。 – Win 2014-09-13 16:01:07