关于网友提出的“数据库保存session 用session保存用户信息的时候提示“当前上下文中不存在名称session””问题疑问,本网通过在网上对“数据库保存session 用session保存用户信息的时候提示“当前上下文中不存在名称session””有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:数据库保存session 用session保存用户信息的时候提示“当前上下文中不存在名称session”描述:
我在一个login页面中打算用session保存登录成功的用户的用户名,但是提示未定义。但是在default.aspx中用来读取和显示的语句就没有这样的提示。请问各位这个要怎么解决呢?
代码大概是这样的:
//login.aspx.cs
private bool SiteLevelCustomAuthenticationMethod(string UserName,string Password)
{
bool boolReturnValue =false;
// Insert code that implements a site-specific custom
// authentication method here.
// This example implementation always returns false.
string strConnection = "server=WIN-VM5GVPGG2R3;database=laborage;integrated security=true;";
SqlConnection Connection =new SqlConnection(strConnection);
String strSQL = "Select * From ADMINISTRATOR";
SqlCommand command =new SqlCommand(strSQL, Connection);
SqlDataReader Dr;
Connection.Open();
Dr=command.ExecuteReader();
while(Dr.Read())
{
if((UserName == Dr["ID"].ToString()) & (Password == Dr["PWD"].ToString()))
{
boolReturnValue =true;
Session("admin") = Dr["ADNAME"];
}
Dr.Close();
return boolReturnValue;
}
return false;
}
执行的时候提示了错误
default.aspx.cs中有写这样的(我是把这个显示用户信息的代码写在一个master文件里然后套到主页上面的)
if (Session["admin"] == null) Response.Redirect("~/login.aspx");
这个地方没有报错……
这到底是怎么回事呢?请各位帮帮忙~~~
解决方案1:
yes 解决方案2:
Session("admin") 该为 Session["admin"]
解决方案3:括号换下..
解决方案4:哈哈,他们都说了,小括好改为中括号就可以了,还有楼主,你用cooiker做不是很好吗,session不好奥!
解决方案5: 这样的登陆代码,安全性不高,。
顶楼上的Session("admin") = Dr["ADNAME"];
用中括号!
Session("admin") = Dr["ADNAME"];
这里括号啊!Session["admin"]= Dr["ADNAME"];
Session("admin") = Dr["ADNAME"];
Session["admin"] = Dr["ADNAME"];
session[] 是中括号呦
如果其他地方有少个分号什么的 有时也会在session下面打横线
()=>[]