0

I have been have bad behavior at different times using this:

FormsAuthentication.SetAuthCookie(user.UserName, true);

How does/will .Net set the cookie otherwise?

I have tried this: (System.Web.HttpContext.Current.User.Identity.IsAuthenticated fails sometimes)

But my User.Identity.IsAuthenticated is always false

What gives?

Community
  • 1
  • 1
cdub
  • 24,555
  • 57
  • 174
  • 303
  • The reason why SetAuthCookie does not work is because you do not have forms authetocation enabled in web config. Include follwoing setting in your web config within system.web tags: `` – Sunil Jan 14 '16 at 01:47
  • i always had it and it mostly worked – cdub Jan 14 '16 at 01:54
  • 1
    Remember that you can also set the cookie by simply calling `FormsAuthentication.RedirectFromLoginPage` method, which will set the authentication ticket/cookie as well as redirect the use to requested URL or default URL. – Sunil Jan 14 '16 at 02:23
  • Yeah i did that. So that doesn't set the cookie manually and sets it by itself then? – cdub Jan 14 '16 at 02:28
  • Yes, that method encapsulates cookie creation functionality. – Sunil Jan 14 '16 at 03:56

1 Answers1

0

First make sure that forms authentication is enabled in your web config file.

<authentication mode="Forms" />

Use code like below to make it work. The method RedirectFromLoginPage will create the authentication cookie as well as redirect the user to the original page or the default URL i.e. home page.

if (Membership.ValidateUser(userName, password))//or whatever method you use
 {
    // Log the user into the site
    FormsAuthentication.RedirectFromLoginPage(userName, false);
 }
Sunil
  • 20,653
  • 28
  • 112
  • 197