No persistant Cookie created for User

Aug 16, 2010 at 6:29 PM
Edited Aug 16, 2010 at 6:36 PM

When user selects "remember me", persistant cookie is not created.

Change code in FormsAuthenticationService.cs:

public TUser Login(string userName, string password, bool isPersistent, string customData) {
            TUser user = ValidateCredentials(userName, password, customData);

            if (user != null) {
                string userData = SerializeUser(user);

                FormsAuthenticationTicket ticket =
                    ticket = new FormsAuthenticationTicket(/* version */ 2, userName,
                                                           DateTime.Now, DateTime.Now.Add(CookieLifetime),
                                                           isPersistent,
                                                           userData,
                                                           FormsAuthentication.FormsCookiePath);

                string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                //HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                HttpCookie authCookie = FormsAuthentication.GetAuthCookie(userName, isPersistent,FormsAuthentication.FormsCookiePath);
                authCookie.Expires = DateTime.Now.Add(CookieLifetime);
                authCookie.Value = encryptedTicket;
                HttpContextBase httpContext = (HttpContextBase)ServiceContext.GetService(typeof(HttpContextBase));
                
                httpContext.Response.Cookies.Add(authCookie);

                if (_enableRoles) {
                    FormsAuthenticationHelper.Enable(this.GetType());
                }
            }

            return user;
        }
FormsAuthentication.GetAuthCookie(userName,isPersistent) creates the persistent cookie. The original code does not create the persistent cookie.
Aug 17, 2010 at 12:13 PM
Edited Aug 17, 2010 at 12:19 PM
public TUser Login(string userName, string password, bool isPersistent, string customData)
        {
            TUser user = ValidateCredentials(userName, password, customData);

            if (user != null)
            {
                string userData = SerializeUser(user);

                FormsAuthenticationTicket ticket =
                    ticket = new FormsAuthenticationTicket(/* version */ 1, userName,
                                                           DateTime.Now, DateTime.Now.Add(CookieLifetime),
                                                           isPersistent,
                                                           userData,
                                                           FormsAuthentication.FormsCookiePath);

                string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                if (isPersistent)
                    // For some reason "Expires must be set for the cookie itself, or it will not persist across sessions
                    authCookie.Expires = DateTime.Now.Add(CookieLifetime);
                HttpContextBase httpContext = (HttpContextBase)ServiceContext.GetService(typeof(HttpContextBase));

                httpContext.Response.Cookies.Add(authCookie);

                if (_enableRoles)
                {
                    FormsAuthenticationHelper.Enable(this.GetType());
                }
            }

            return user;
        }