我有一个运行在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"] = "";
}
}
}发布于 2013-04-07 06:43:12
您的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的值。
https://stackoverflow.com/questions/15856790
复制相似问题