从.Net MVC应用程序

时间:2017-02-20 07:52:02

标签: asp.net asp.net-mvc cookies session-cookies form-authentication

我最近的一个基于.Net MVC的Web应用程序我有一种情况,我需要使用多个身份验证cookie,如

1表单Auth Cookie

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddDays(30), RememberMe, 'data1', FormsAuthentication.FormsCookiePath);
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
HttpContext.Current.Response.Cookies.Add(cookie);

2自定义身份验证Cookie

FormsAuthenticationTicket cstTicket = new FormsAuthenticationTicket(1, Username, DateTime.Now, DateTime.Now.AddDays(30), isPersistent, 'data 2', FormsAuthentication.FormsCookiePath);
string _encryptedTicket = FormsAuthentication.Encrypt(cstTicket);
HttpCookie cstCookie = new HttpCookie("iCstm", _encryptedTicket);
HttpContext.Current.Response.Cookies.Add(cstCookie);

当有人从应用程序注销时,我需要删除这两个cookie。如果我调用FormsAuthentication.SignOut();然后它删除基本的auth cookie。我尝试通过将Expires值设置为

来删除第二个cookie
HttpContext.Current.Request.Cookies["iCstm"].Expires = DateTime.UtcNow.AddYears(-1);
HttpContext.Current.Response.Cookies["iCstm"].Value = "";
HttpContext.Current.Response.Cookies.Add(HttpContext.Current.Request.Cookies["iCstm"]);

但是在auth cookie的情况下这不起作用。有没有办法解决这个问题。

提前致谢。

0 个答案:

没有答案