登录用户的名称未显示

时间:2016-05-25 11:41:23

标签: asp.net login

我开发了一个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

5 个答案:

答案 0 :(得分: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();
        }
    }
}

答案 1 :(得分: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是一篇解释前两点的文章。

答案 2 :(得分: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");
        }

答案 3 :(得分:0)

最后我找到了解决方案, 我没有为 DisplayName Col

正确添加SP
AS
BEGIN

    SELECT 
        UserId,
        UserName,
        FullName,
        UserCatagoryId,
         DisplayName

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


END

答案 4 :(得分:-1)

希望您的登录代码正常工作,然后只需使用

Session["DisplayName"] = txtUserName.Text;

而不是

Session["DisplayName"] = dtUser.Rows[0]["DisplayName"].ToString();
相关问题