Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

Using Forms Authentication with rememebr me

$
0
0

Hi,

I am little confused and all the examples i have seen so far are handling remember me and not remember me differently. I have the following code, how can i consolidate the two together?

if (!rememberMe)
                    {
                        //Set cookie
                        FormsAuthentication.SetAuthCookie(userId, false);
                        GenericIdentity identity = new GenericIdentity(userId);
                        string[] roles = { userRole }; //or do it from the person object
                        GenericPrincipal principal = new GenericPrincipal(identity, roles);
                        HttpContext.Current.User = principal;
                    }
                    else
                    {
                        //Create Persistent cookie
                        var ticket = new FormsAuthenticationTicket(
                           1, // Ticket version
                           userId, // Username associated with ticket
                           DateTime.Now, // Date/time issued
                           DateTime.Now.AddYears(1), // Date/time to expire
                           true, // "true" for a persistent user cookie
                           userRole, // User-data, in this case the roles
                           FormsAuthentication.FormsCookiePath);// Path cookie valid for

                        // Encrypt the cookie using the machine key for secure transport
                        string hash = FormsAuthentication.Encrypt(ticket);
                        HttpCookie cookie = new HttpCookie(
                            FormsAuthentication.FormsCookieName, // Name of auth cookie
                            hash); // Hashed ticket

                        // Set the cookie's expiration time to the tickets expiration time
                        if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

                        // Add the cookie to the list for outgoing response
                        HttpContext.Current.Response.Cookies.Add(cookie);
                    }


Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>