退出的时候是应该清空cookie的值,还是应该直接cookie.Values.Add("Id", entity.Id.ToString()),这个Id等于0来表示退出的。
请问该是清空还是直接覆盖??现在我出现了个奇怪的问题
9 个解决方案
#1
this.Request.Cookies["adminusers"].Expires = DateTime.Now.AddHours(-1); // this.Response.Cookies.Remove("adminusers"); Response.Cookies.Add(Request.Cookies["adminusers"]);
#2
public void SetCurrentUsers(Users entity)
{
System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies["Users"];
if (cookie == null)
{
cookie = new System.Web.HttpCookie("Users");
}
System.Web.HttpContext.Current.Response.Cookies["Id"].Expires = DateTime.Now.AddDays(-1);
System.Web.HttpContext.Current.Response.Cookies["Username"].Expires = DateTime.Now.AddDays(-1);
cookie.Values.Add("Id", entity.Id.ToString());
cookie.Values.Add("Username", entity.Username.ToString());
System.Web.HttpContext.Current.Response.AppendCookie(cookie);
}
这个是我设置cookie的,第一次登录的时候Id的值是8,退出的时候,值变成了8,-1 啊啊!!!
#3
我是这样做的
private bool SetLoginCookie(UserAccount account, bool isPersistent, string returnUrl)
{
bool loginSuccess = false;
DateTime expirationTime = DateTime.Now.AddMinutes(60);
if (isPersistent)
{
expirationTime = DateTime.Now.AddMinutes(14400);
}
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, // version
account.UserId.ToString(),
DateTime.Now,
expirationTime,
isPersistent,
GetRolesByUserId(account.UserId, ';')
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
authCookie.Path = FormsAuthentication.FormsCookiePath;
if (isPersistent)
{
authCookie.Expires = expirationTime;
}
HttpContext current = HttpContext.Current;
if (current != null)
{
authCookie.Domain = FormsAuthentication.CookieDomain;
current.Response.Cookies.Add(authCookie);
if (!string.IsNullOrEmpty(returnUrl))
{
current.Response.Redirect(returnUrl, false);
}
loginSuccess = true;
}
return loginSuccess;
}
private bool SetLoginCookie(UserAccount account, bool isPersistent, string returnUrl)
{
bool loginSuccess = false;
DateTime expirationTime = DateTime.Now.AddMinutes(60);
if (isPersistent)
{
expirationTime = DateTime.Now.AddMinutes(14400);
}
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, // version
account.UserId.ToString(),
DateTime.Now,
expirationTime,
isPersistent,
GetRolesByUserId(account.UserId, ';')
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
authCookie.Path = FormsAuthentication.FormsCookiePath;
if (isPersistent)
{
authCookie.Expires = expirationTime;
}
HttpContext current = HttpContext.Current;
if (current != null)
{
authCookie.Domain = FormsAuthentication.CookieDomain;
current.Response.Cookies.Add(authCookie);
if (!string.IsNullOrEmpty(returnUrl))
{
current.Response.Redirect(returnUrl, false);
}
loginSuccess = true;
}
return loginSuccess;
}
#4
正解
#5
我想问一下,我在A.aspx的页面里调用System.Web.HttpContext.Current.Request.Cookies["Users"];的Remove方法,跟B.aspx的页面里调用System.Web.HttpContext.Current.Request.Cookies["Users"];这个Request和Response是同一个吗??
#6
人家是在提问 你还正解 你知道什么意思?
#7
this.Request.Cookies["adminusers"].Expires = DateTime.Now.AddHours(-1);
用来定义COOKIES失效
用来定义COOKIES失效
#8
定义失效时间就可以了
#9
这个可以
#1
this.Request.Cookies["adminusers"].Expires = DateTime.Now.AddHours(-1); // this.Response.Cookies.Remove("adminusers"); Response.Cookies.Add(Request.Cookies["adminusers"]);
#2
public void SetCurrentUsers(Users entity)
{
System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies["Users"];
if (cookie == null)
{
cookie = new System.Web.HttpCookie("Users");
}
System.Web.HttpContext.Current.Response.Cookies["Id"].Expires = DateTime.Now.AddDays(-1);
System.Web.HttpContext.Current.Response.Cookies["Username"].Expires = DateTime.Now.AddDays(-1);
cookie.Values.Add("Id", entity.Id.ToString());
cookie.Values.Add("Username", entity.Username.ToString());
System.Web.HttpContext.Current.Response.AppendCookie(cookie);
}
这个是我设置cookie的,第一次登录的时候Id的值是8,退出的时候,值变成了8,-1 啊啊!!!
#3
我是这样做的
private bool SetLoginCookie(UserAccount account, bool isPersistent, string returnUrl)
{
bool loginSuccess = false;
DateTime expirationTime = DateTime.Now.AddMinutes(60);
if (isPersistent)
{
expirationTime = DateTime.Now.AddMinutes(14400);
}
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, // version
account.UserId.ToString(),
DateTime.Now,
expirationTime,
isPersistent,
GetRolesByUserId(account.UserId, ';')
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
authCookie.Path = FormsAuthentication.FormsCookiePath;
if (isPersistent)
{
authCookie.Expires = expirationTime;
}
HttpContext current = HttpContext.Current;
if (current != null)
{
authCookie.Domain = FormsAuthentication.CookieDomain;
current.Response.Cookies.Add(authCookie);
if (!string.IsNullOrEmpty(returnUrl))
{
current.Response.Redirect(returnUrl, false);
}
loginSuccess = true;
}
return loginSuccess;
}
private bool SetLoginCookie(UserAccount account, bool isPersistent, string returnUrl)
{
bool loginSuccess = false;
DateTime expirationTime = DateTime.Now.AddMinutes(60);
if (isPersistent)
{
expirationTime = DateTime.Now.AddMinutes(14400);
}
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, // version
account.UserId.ToString(),
DateTime.Now,
expirationTime,
isPersistent,
GetRolesByUserId(account.UserId, ';')
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
authCookie.Path = FormsAuthentication.FormsCookiePath;
if (isPersistent)
{
authCookie.Expires = expirationTime;
}
HttpContext current = HttpContext.Current;
if (current != null)
{
authCookie.Domain = FormsAuthentication.CookieDomain;
current.Response.Cookies.Add(authCookie);
if (!string.IsNullOrEmpty(returnUrl))
{
current.Response.Redirect(returnUrl, false);
}
loginSuccess = true;
}
return loginSuccess;
}
#4
正解
#5
我想问一下,我在A.aspx的页面里调用System.Web.HttpContext.Current.Request.Cookies["Users"];的Remove方法,跟B.aspx的页面里调用System.Web.HttpContext.Current.Request.Cookies["Users"];这个Request和Response是同一个吗??
#6
人家是在提问 你还正解 你知道什么意思?
#7
this.Request.Cookies["adminusers"].Expires = DateTime.Now.AddHours(-1);
用来定义COOKIES失效
用来定义COOKIES失效
#8
定义失效时间就可以了
#9
这个可以