Cookie过期日期不起作用C#

RJV*_*RJV 0 c# asp.net asp.net-mvc

我正在创建一个在30天后到期的cookie.

这是c#
e.UserID中的代码- > username,e.rememberMe - > true

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                                                        "UserID",
                                                        DateTime.Now,
                                                        DateTime.Now.AddDays(30),
                                                        e.rememberMe,
                                                        e.UserID,
                                                        FormsAuthentication.FormsCookiePath);
                // Encrypt the ticket.
                string encTicket = FormsAuthentication.Encrypt(ticket);
                // Create the cookie.
                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
Run Code Online (Sandbox Code Playgroud)

但是在chrome中,cookie存储到浏览器会话结束 在此输入图像描述

任何的想法

Pet*_*r B 9

您应该设置Expirescookie对象的属性,因为cookie不知道您放入其中的内容:

Response.Cookies.Add(
    new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) {
        Expires = ticket.Expiration
    });
Run Code Online (Sandbox Code Playgroud)