用session保存用户信息的时候提示“当前上下文中不存在名称session”

sky_pearl sky_pearl 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我在一个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");

这个地方没有报错……

这到底是怎么回事呢?请各位帮帮忙~~~
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
sky_pearl
.NET社区 62,274 成员 668,968 内容