2016-05-25 114 views
4

我開發了一個ASP.NET Web應用程序。登錄頁面和註銷功能正常工作。我添加了一個新的方法來顯示登錄用戶的名稱。添加此方法後,我無法登錄到我的應用程序。我不認爲我已經正確地添加了這個方法。 Login 你能幫忙嗎?登錄用戶名不顯示

的Login.aspx

protected void btnLogin_Click(object sender, EventArgs e) 
{ 
    try 
    { 
    DataTable dtUser = UserRegistration.GetUserByUserName(txtUserName.Text, txtPassword.Text); 
    if (dtUser.Rows.Count > 0) 
    { 
     lblSuccessMessage.Text = "Login Successful!"; 

     oLoginData = txtUserName.Text; 

     Session["intUserId"] = dtUser.Rows[0]["intUserId"].ToString(); 
     Session["DisplayName"] = dtUser.Rows[0]["DisplayName"].ToString(); 
     Response.Redirect("~/WebForms/Home/Home.aspx"); 

    } 
    else 
    { 
     lblErrorMessage.Text = "Incorrect User Name or Password"; 
     txtUserName.BackColor = System.Drawing.Color.LavenderBlush; 
     txtPassword.BackColor = System.Drawing.Color.LavenderBlush; 
     return; 
    } 
    } 
    catch 
    { 
    lblErrorMessage.Text = "Incorrect User Name or Password"; 
    txtUserName.BackColor = System.Drawing.Color.LavenderBlush; 
    txtPassword.BackColor = System.Drawing.Color.LavenderBlush; 
    return; 
    } 
} 
} 

Site.Masters.cs

protected void Page_Load(object sender, EventArgs e) 
{ 
    if ((Session["intUserId"] == null)) 
    { 
    FormsAuthentication.SignOut(); 
    Response.Redirect("~/Login.aspx"); 
    } 
    else 
    { 
    //lblLocation.Text = Session["LocationName"].ToString(); 
    lblUser.Text = "Logged User :" + Session["DisplayName"].ToString(); 
    } 
} 

GetUserByUserName

public static DataTable GetUserByUserName(string UserName, string Password) 
     { 
      DataTable dsResult = new DataTable(); 
      try 
      { 


       String strConnString = ConfigurationManager.ConnectionStrings["TCDMSConnection"].ConnectionString; 
       SqlConnection con = new SqlConnection(strConnString); 
       SqlCommand com = new SqlCommand(); 
       SqlDataAdapter da = new SqlDataAdapter(); 

       con.Open(); 
       com.Connection = con; 
       com.CommandType = CommandType.StoredProcedure; 
       com.CommandText = "spUserValidation"; 

       SqlParameter[] sqlParam = new SqlParameter[2]; 

       sqlParam[0] = new SqlParameter("@Username", UserName); 
       sqlParam[1] = new SqlParameter("@Password", Password); 

       if (sqlParam != null) 
       { 
        com.Parameters.AddRange(sqlParam); 
       } 

       da.SelectCommand = com; 
       da.Fill(dsResult); 

       con.Close(); 

       return dsResult; 

      } 
      catch (Exception ex) 
      { 
       throw (ex); 
      } 




     } 


    } 

Image

+0

當會話爲空時,站點母版頁加載時發生了什麼?你得到顯示名稱?並且你寫了無法登錄,請解釋,並且在調試時出現了什麼錯誤。 – Ajay2707

+0

我不能登錄我的應用程序,沒有任何錯誤,登錄頁面顯示登錄成功消息,但不能登錄 – Codeone

+0

我想使用戶登錄,顯示當前用戶名後 – Codeone

回答

0

最後我找到了解決辦法, im不能正確添加SP爲Disp layNameCol

AS 
BEGIN 

    SELECT 
     UserId, 
     UserName, 
     FullName, 
     UserCatagoryId, 
     DisplayName 

    FROM UserRegistrations 
    WHERE UserName = @Username AND Password = @Password 


END 
4

如果你的登錄頁面Login.aspx使用相同的母版頁Site.Master那麼你似乎有重定向循環。

當您訪問該應用程序時,由於您尚未登錄,您將被重定向到Login.aspx。當Login.aspx正在加載,這一部分:

if ((Session["intUserId"] == null)) 
{ 
    FormsAuthentication.SignOut(); 
    Response.Redirect("~/Login.aspx"); 
} 
母版頁將簽署並導致重定向回 Login.aspx,這將導致整個事情重複的

瀏覽器會注意到該請求被重定向很多倍,並會最終放棄,並顯示某種頁面無法顯示錯誤。

要修復它,您需要避免重定向,如果當前正在運行的頁面已經是Login.aspx。例如像這樣(在Site.Master.cs):

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Request.CurrentExecutionFilePath.ToLower().EndsWith("login.aspx")) 
    { 
     if ((Session["intUserId"] == null)) 
     { 
      FormsAuthentication.SignOut(); 
      Response.Redirect("~/Login.aspx"); 
     } 
     else 
     { 
      lblUser.Text = "Logged User :" + Session["DisplayName"].ToString(); 
     } 
    } 
} 
+0

感謝幫助爵士而言,我嘗試這一點,但無法正常工作(不正確的用戶名和密碼登錄成功!)顯示登錄頁面 – Codeone

+0

您是否獲得登錄成功消息?或不正確的用戶...消息?或兩者? – sam

+0

我同時收到兩條消息 – Codeone

2

根據你的意見,你沒有得到任何exception和代碼工作正常。所以你可以嘗試的東西很少。

  • 請勿使用Response.Redirect("yourpage")改爲使用Response.Redirect("yourpage",false);
  • 使用FormsAuthentication.RedirectFromLoginPage方法重定向 登錄頁面而不是Response.Redirect()
  • 另外,請檢查你的web.config文件,並確保您有啓用 會話的timeout財產和cookiesless會議有效 值。

    <sessionState 
        mode="InProc" 
        cookieless="false" 
        timeout="100"/> 
    

Here爲一種製品,說明第一個兩分。

-1

希望您的登錄代碼工作正常,然後只需使用

Session["DisplayName"] = txtUserName.Text; 

,而不是

Session["DisplayName"] = dtUser.Rows[0]["DisplayName"].ToString(); 
+0

我用它,但無法正常工作 – Codeone

1

[assuming your GetUserByUserName method works fine]嘗試Page_Load方法改變你

if (Session["intUserId"] != null) 
     { 
      if (string.IsNullOrEmpty(Session["DisplayName"].ToString())) 
      { 
       // Your Display name is having blank space , null value 
      } 
      else 
      { 
       //lblLocation.Text = Session["LocationName"].ToString(); 
       lblUser.Text = "Logged User :" + Session["DisplayName"].ToString(); 
      } 

     } 
     else 
     { 
      FormsAuthentication.SignOut(); 
      Response.Redirect("~/Login.aspx"); 
     } 
+0

林爵士試圖做到這一點,但不能登錄 – Codeone

+0

您無法登錄,或者無法顯示用戶名? – KanisXXX

+0

爵士,我使其 – Codeone