会话在返回主页时消失

时间:2013-04-06 22:32:44

标签: asp.net

我有一个在Azure上运行的网站,用户可以登录该网站然后导航到其他页面(当然)。我的问题是,当我返回索引/主页时,会话就会消失。我认为我在后面的代码中与登录控件及其身份验证方法有关,但我尝试在另一个页面上使用相同的身份验证事件进行另一次登录,但这完全没问题。

我没有找到任何有类似问题的人。

这是index.aspx背后的代码

string Connection = ConfigurationManager.ConnectionStrings["****"].ConnectionString;
protected void Page_Load(object sender, EventArgs e) {}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) {
  string Username = Login1.UserName;
  string pwd = Login1.Password;
  SqlConnection connection = new SqlConnection(Connection);
  connection.Open();
  //SqlCommand comm = new SqlCommand("SELECT COUNT([*****], [*****]) FROM ***** WHERE [****] = '***' AND [****] = '****'", connection);
  string sqlUserName = "SELECT [****] ,[****] FROM ***** WHERE [*****] ='" + * * * * * +"' AND [*****] ='" + * * * +"'";
  SqlCommand cmd = new SqlCommand(sqlUserName, connection);
  string CurrentName;
  CurrentName = (string) cmd.ExecuteScalar();
  if(CurrentName != null) {
    Login1.FailureText = "Welcome";
    Session["User"] = Username;
    Session["LoggedIn"] = true;
    Label1.Text = Session["User"].ToString();
    if((bool) Session["LoggedIn"] == true && Session["User"].ToString() == "admin1") {
      HyperLink3.Visible = true;
    } else if((bool) Session["LoggedIn"] == true) {
      HyperLink1.Visible = true;
    }
  } else {
    Session["User"] = "";
  }
}
}

1 个答案:

答案 0 :(得分:0)

你的if语句必须在某处窃听,或者CurrentName为空。

if (CurrentName != null)
        {
            Login1.FailureText = "Welcome";
            Session["User"] = Username;
            Session["LoggedIn"] = true ;
            Label1.Text = Session["User"].ToString();
            if ((bool)Session["LoggedIn"] == true && Session["User"].ToString() == "admin1")
            {
                HyperLink3.Visible = true;
            }
            else if ((bool)Session["LoggedIn"] == true)
            {
                HyperLink1.Visible = true;
            }
        }

        else
        {
            Session["User"] = "";

        }

最可能的罪魁祸首是前面的SQL查询。使用SQL查询仔细检查语法。我不确定你在那里组合了哪些星号变量,但它们可能会导致问题。您应该继续对该脚本进行逐行调试。在中途抓住并检查CurrentName的值。