ASP.NET don';关闭浏览器时无法清除Cookie会话

ASP.NET don';关闭浏览器时无法清除Cookie会话,asp.net,session,cookies,Asp.net,Session,Cookies,我想在关闭浏览器时保留会话cookie 以下是cookie会话的配置部分: <authentication mode="Forms"> <forms name=".ASPXFORMSAUTH" cookieless="AutoDetect" defaultUrl="~/Default.aspx" loginUrl="~/Account/Login.aspx" timeout="99999999" /> </authentication> 我用Chro

我想在关闭浏览器时保留会话cookie

以下是cookie会话的配置部分:

<authentication mode="Forms">
  <forms name=".ASPXFORMSAUTH" cookieless="AutoDetect" defaultUrl="~/Default.aspx" loginUrl="~/Account/Login.aspx" timeout="99999999" />
</authentication>

我用Chrome进行测试,没问题,但在Firefox和IE中,当我关闭浏览器时,cookie会被删除


谢谢。

根据设计,ASP.NET会话状态使用非持久性会话cookie,当您关闭浏览器时,这些cookie将失效

您需要一种使ASP.NET会话cookie持久化的方法

创建持久cookie。

//create a cookie
HttpCookie myCookie = new HttpCookie("myCookie");

//Add key-values in the cookie
myCookie.Values.Add("userid", "USER_ID_HERE");

//set cookie expiry date-time. Made it to last for next 12 hours.
myCookie.Expires = DateTime.Now.AddHours(12);

//Most important, write the cookie to client.
Response.Cookies.Add(myCookie);
//Assuming user comes back after several hours. several < 12.
//Read the cookie from Request.
HttpCookie myCookie = Request.Cookies["myCookie"];
if (myCookie == null)
{
    //No cookie found or cookie expired.
    //Handle the situation here, Redirect the user or simply return;
}

//ok - cookie is found.
//Gracefully check if the cookie has the key-value as expected.
if (!string.IsNullOrEmpty(myCookie.Values["userid"]))
{
    string userId = myCookie.Values["userid"].ToString();
    //Yes userId is found. Mission accomplished.
}
读取持久cookie。

//create a cookie
HttpCookie myCookie = new HttpCookie("myCookie");

//Add key-values in the cookie
myCookie.Values.Add("userid", "USER_ID_HERE");

//set cookie expiry date-time. Made it to last for next 12 hours.
myCookie.Expires = DateTime.Now.AddHours(12);

//Most important, write the cookie to client.
Response.Cookies.Add(myCookie);
//Assuming user comes back after several hours. several < 12.
//Read the cookie from Request.
HttpCookie myCookie = Request.Cookies["myCookie"];
if (myCookie == null)
{
    //No cookie found or cookie expired.
    //Handle the situation here, Redirect the user or simply return;
}

//ok - cookie is found.
//Gracefully check if the cookie has the key-value as expected.
if (!string.IsNullOrEmpty(myCookie.Values["userid"]))
{
    string userId = myCookie.Values["userid"].ToString();
    //Yes userId is found. Mission accomplished.
}
//假设用户几个小时后回来。有好几个<12。
//从请求中读取cookie。
HttpCookie mycokie=Request.Cookies[“mycokie”];
如果(mycokie==null)
{
//未找到cookie或cookie已过期。
//在这里处理情况,重定向用户或直接返回;
}
//确定-找到cookie。
//优雅地检查cookie是否具有预期的键值。
如果(!string.IsNullOrEmpty(mycokie.Values[“userid”]))
{
字符串userId=mycokie.Values[“userId”].ToString();
//是,找到用户ID。任务完成。
}

同样

根据设计,ASP.NET会话状态使用非持久性会话cookie,当您关闭浏览器时,这些cookie将失效

您需要一种使ASP.NET会话cookie持久化的方法

创建持久cookie。

//create a cookie
HttpCookie myCookie = new HttpCookie("myCookie");

//Add key-values in the cookie
myCookie.Values.Add("userid", "USER_ID_HERE");

//set cookie expiry date-time. Made it to last for next 12 hours.
myCookie.Expires = DateTime.Now.AddHours(12);

//Most important, write the cookie to client.
Response.Cookies.Add(myCookie);
//Assuming user comes back after several hours. several < 12.
//Read the cookie from Request.
HttpCookie myCookie = Request.Cookies["myCookie"];
if (myCookie == null)
{
    //No cookie found or cookie expired.
    //Handle the situation here, Redirect the user or simply return;
}

//ok - cookie is found.
//Gracefully check if the cookie has the key-value as expected.
if (!string.IsNullOrEmpty(myCookie.Values["userid"]))
{
    string userId = myCookie.Values["userid"].ToString();
    //Yes userId is found. Mission accomplished.
}
读取持久cookie。

//create a cookie
HttpCookie myCookie = new HttpCookie("myCookie");

//Add key-values in the cookie
myCookie.Values.Add("userid", "USER_ID_HERE");

//set cookie expiry date-time. Made it to last for next 12 hours.
myCookie.Expires = DateTime.Now.AddHours(12);

//Most important, write the cookie to client.
Response.Cookies.Add(myCookie);
//Assuming user comes back after several hours. several < 12.
//Read the cookie from Request.
HttpCookie myCookie = Request.Cookies["myCookie"];
if (myCookie == null)
{
    //No cookie found or cookie expired.
    //Handle the situation here, Redirect the user or simply return;
}

//ok - cookie is found.
//Gracefully check if the cookie has the key-value as expected.
if (!string.IsNullOrEmpty(myCookie.Values["userid"]))
{
    string userId = myCookie.Values["userid"].ToString();
    //Yes userId is found. Mission accomplished.
}
//假设用户几个小时后回来。有好几个<12。
//从请求中读取cookie。
HttpCookie mycokie=Request.Cookies[“mycokie”];
如果(mycokie==null)
{
//未找到cookie或cookie已过期。
//在这里处理情况,重定向用户或直接返回;
}
//确定-找到cookie。
//优雅地检查cookie是否具有预期的键值。
如果(!string.IsNullOrEmpty(mycokie.Values[“userid”]))
{
字符串userId=mycokie.Values[“userId”].ToString();
//是,找到用户ID。任务完成。
}

还有

您正在创建会话cookie的cookie是否是偶然的?您正在创建会话cookie的cookie是否是偶然的?谢谢Andrew。谢谢Andrew。