首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >会话在返回主页时消失

问会话在返回主页时消失
EN

Stack Overflow用户
提问于 2013-04-07 06:32:44
回答 1查看 57关注 0票数 0

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

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

下面是index.aspx的代码

代码语言:javascript
复制
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"] = "";
  }
}
}
EN

回答 1

Stack Overflow用户

发布于 2013-04-07 06:43:12

您的if语句一定是出了问题,或者CurrentName为空。

代码语言:javascript
复制
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的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15856790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档