2

I know two way for authentication: first: using FormsAuthentication:

FormsAuthentication.SetAuthCookie(login.UserName, login.RememberMe);

and the other way : using FormsAuthenticationTicket

  var ticket = new FormsAuthenticationTicket(1, user.Email.ToString(), DateTime.Now, DateTime.Now.AddSeconds(300000), login.RememberMe, "a");
                var ticketEncrypted = FormsAuthentication.Encrypt(ticket);
                var cookie = new HttpCookie("eshop", ticketEncrypted);
                if (login.RememberMe)
                {
                    cookie.Expires = DateTime.Now.AddMinutes(100);
                }
                HttpContext.Response.Cookies.Add(cookie);

i want to know which one is better? and when i should use first one and when the other way?

1
  • suggest changing the title of your post to something that better reflects what you're asking Commented Jun 10, 2016 at 7:48

1 Answer 1

3

The first method also uses FormsAuthenticationTicket - follow the source code to FormsAuthentication.GetAuthCookie and you can see

FormsAuthenticationTicket ticket = FormsAuthenticationTicket.FromUtc(2, userName, utcNow, expirationUtc, createPersistentCookie, string.Empty, strCookiePath);
string str = FormsAuthentication.Encrypt(ticket, hexEncodedTicket);

i.e. starting to look similar to your second code sample

Unless you are using cookieless auth then both are doing effectively the same thing i.e. setting a cookie in the browser

You might use the latter if you wanted finer control over what attributes the cookie has.

Sign up to request clarification or add additional context in comments.

2 Comments

are you mean both ways are same, and first way just encapsulate second way?
what i'm saying is that under certain conditions they will lead to the same result. the former has the ability to do cookieless which is entirely different to using cookies but assuming you DO use cookies (which most do) then yes the former encapsulates the latter (maybe not exactly as you wrote in your sample but it has the potential)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.