2014-09-03 33 views
0

我正在用Login控件創建一個SignIn頁面。我希望頁面在被認證後重定向到另一個頁面。但它將我重定向到主頁。(home.aspx)。奇怪。請指教。Response.Redirect不工作onloggedin事件

<asp:Login ID="Login1" runat="server" OnAuthenticate="ValidateUser" OnLoggedIn="Login1_LoggedIn" DestinationPageUrl="~/DonationForm.aspx"> 
    </asp:Login> 

public partial class Login : System.Web.UI.Page 
{ 

    protected void ValidateUser(object sender, EventArgs e) 
    { 
     int userId = 0; 
     string constr = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString; 
     using (SqlConnection con = new SqlConnection(constr)) 
     { 
      using (SqlCommand cmd = new SqlCommand("Validate_User")) 
      { 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.Parameters.AddWithValue("@Username", Login1.UserName); 
       cmd.Parameters.AddWithValue("@Password", Login1.Password); 
       cmd.Connection = con; 
       con.Open(); 
       userId = Convert.ToInt32(cmd.ExecuteScalar()); 
       con.Close(); 
      } 
      switch (userId) 
      { 
       case -1: 
        Login1.FailureText = "Username and/or password is incorrect."; 
        break; 
       case -2: 
        Login1.FailureText = "Account has not been activated."; 
        break; 
       default: 
        FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet); 
        break; 
      } 
     } 
    } 



    protected void Login1_LoggedIn(object sender, EventArgs e) 
    { 
     Response.Redirect("~/DonationForm.aspx"); 
    } 

}

+0

user3115435 2014-09-03 21:02:33

+0

您是否使用表單身份驗證?你能粘貼web.config的代碼部分嗎? – Partha 2014-09-03 21:24:44

+1

它開始爲我工作。我更改了defaulturl =「〜DonationForm.aspx」 user3115435 2014-09-03 21:38:11

回答

1

這是你在switch語句中有FormsAuthentication.RedirectFromLoginPage方法的正常行爲。它重定向回原來指向登錄頁面的頁面。在查看登錄頁面時,您可以在ReturnUrl QueryString值中看到頁面名稱。

如果要重定向到另一頁請嘗試更改switch語句的默認塊

default: 
    FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet); 
    Response.Redirect("~/DonationForm.aspx"); 
    break; 
0

在您的情況,您要使用LoggingIn事件,因爲你自己驗證用戶,而不是依靠會員供應商

如果您使用以下LoggingIn事件,你不需要的ValidateUser的loggedIn事件。

protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e) 
{ 
    int userId = 0; 
    string constr = ConfigurationManager.ConnectionStrings 
     ["DatabaseConnectionString"].ConnectionString; 

    using (SqlConnection con = new SqlConnection(constr)) 
    { 
     using (SqlCommand cmd = new SqlCommand("Validate_User")) 
     { 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.Parameters.AddWithValue("@Username", Login1.UserName); 
      cmd.Parameters.AddWithValue("@Password", Login1.Password); 
      cmd.Connection = con; 
      con.Open(); 
      userId = Convert.ToInt32(cmd.ExecuteScalar()); 
      con.Close(); 
     } 
     switch (userId) 
     { 
      case -1: 
       Login1.FailureText = "Username and/or password is incorrect."; 
       break; 
      case -2: 
       Login1.FailureText = "Account has not been activated."; 
       break; 
      default: 
       // RedirectFromLoginPage will take care of creating 
       // authentication cookie and redirect; you do not need 
       // to do anything. 
       FormsAuthentication.RedirectFromLoginPage(
        Login1.UserName, Login1.RememberMeSet); 
       break; 
     } 
    } 
} 

我想頁面被重定向到另一個頁面,一旦它得到了驗證。 但它將我重定向到主頁。(home.aspx)。奇怪。

如果您希望用戶重定向到某個頁面,您可以在web.config中設置defaultUrl

<authentication mode="Forms" > 
    <forms loginUrl="~/Account/Login" defaultUrl="~/DonationForm.aspx" /> 
</authentication>