Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 登录表单和会话_C#_Asp.net_Session_Login - Fatal编程技术网

C# 登录表单和会话

C# 登录表单和会话,c#,asp.net,session,login,C#,Asp.net,Session,Login,我有一个带有两个框和按钮的登录表单单击我使用sql数据库验证用户并将id存储在会话中,然后设置表单身份验证供以后使用。在“注销”按钮上,我将它们重定向到logout.aspx,在那里我销毁会话并重定向到登录页面: Session.Abandon(); FormsAuthentication.SignOut(); 现在注销后,如果我使用浏览器返回按钮返回,我仍然可以看到我以前的页面,但如果我刷新,我会得到对象引用未设置为对象的实例。 protected void Page_Load(obje

我有一个带有两个框和按钮的登录表单单击我使用sql数据库验证用户并将id存储在会话中,然后设置表单身份验证供以后使用。在“注销”按钮上,我将它们重定向到logout.aspx,在那里我销毁会话并重定向到登录页面:

 Session.Abandon();
 FormsAuthentication.SignOut();
现在注销后,如果我使用浏览器返回按钮返回,我仍然可以看到我以前的页面,但如果我刷新,我会得到
对象引用未设置为对象的实例。

protected void Page_Load(object sender, EventArgs e)
Line 33:         {
Line 34:             string id = Session["ID"].ToString();

如果发生这种情况,将用户发送回登录的最佳解决方法是什么?

您应该首先检查
会话[“ID”]!=空
否则,用户未通过身份验证


但是如果您使用FormsAuthentication,您也可以使用
User.Identity.IsAuthenticated

您应该首先检查
会话[“ID”]!=空
否则,用户未通过身份验证


但是如果你使用FormsAuthentication,你也可以使用
User.Identity.IsAuthenticated

Nota:我建议你使用

if(Session["ID"] != null)
{

}
但我也建议您使用此
配置

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="Login.aspx"
           defaultUrl="default.aspx" />
  </authentication>
</system.web>
注:序列图


注:我建议您使用

if(Session["ID"] != null)
{

}
但我也建议您使用此
配置

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="Login.aspx"
           defaultUrl="default.aspx" />
  </authentication>
</system.web>
注:序列图