Username is not displayed

I developed an ASP.NET web application. The login and logout functions work correctly. I added a new method to display the registered username. After adding this method, I cannot log in to my application. I do not think I added this method correctly. Login Could you help me?

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




        }


    }

Picture

+4
source share
5 answers

Finally, I found a solution, im not correctly adding SP for DisplayName Col

AS
BEGIN

    SELECT 
        UserId,
        UserName,
        FullName,
        UserCatagoryId,
         DisplayName

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


END
0
source

If your login page Login.aspxuses the same main page Site.Master, then you seem to have a redirect cycle.

, 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();
        }
    }
}
+4

exception, . , .

  • Response.Redirect("yourpage") Response.Redirect("yourpage",false);
  • Use the method FormsAuthentication.RedirectFromLoginPageto redirect from the login page Response.Redirect().
  • Also check the web.config file and make sure that you have a valid value for the timeoutsession property and the cookieslesssession is enabled.

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

Here is an article explaining the first two points.

+2
source

[assuming your GetUserByUserName method works fine] Try changing the page_load method to

   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");
        }
+1
source

We hope your login code is working fine, but just use

Session["DisplayName"] = txtUserName.Text;

instead

Session["DisplayName"] = dtUser.Rows[0]["DisplayName"].ToString();
-1
source

All Articles