1

I am using Membership provider but how do I get my username and password to login/signin? When I check to see with this code:

if (User.Identity.IsAuthenticated)
// this code always returns false

I have this before when the user uses asp:Login to login:

if (Membership.ValidateUser(email, password))
{
    // this is true but what am I missing here to make above not be false?

The so called duplicate question/answer uses SetAuthCookie which according to this (System.Web.HttpContext.Current.User.Identity.IsAuthenticated fails sometimes) causes issues and I need to avoid it.

Community
  • 1
  • 1
cdub
  • 21,144
  • 49
  • 157
  • 274
  • 1
    Possible duplicate of [User.Identity.IsAuthenticated is false after successful login](http://stackoverflow.com/questions/17642630/user-identity-isauthenticated-is-false-after-successful-login) – gmiley Jan 13 '16 at 23:53
  • see my duplicate update above. i can't use setauthcookie – cdub Jan 14 '16 at 00:00
  • Use the answer that I provided since it does not use SetAuthCookie. – Sunil Jan 14 '16 at 00:31
  • You need to make sure that forms authentication is enabled in web config as mentioned in my answer. Not doing this is the most common cause of what you are seeing. – Sunil Jan 14 '16 at 01:44

1 Answers1

0

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) defined in web config.

if (Membership.ValidateUser(email, password))
 {
    // Log the user into the site
    FormsAuthentication.RedirectFromLoginPage(email, false);
 }

Also, make sure that forms authentication is enabled in web config. At a minimum at least set mode="Forms" if not other settings under authentication.

<authentication mode="Forms">
  <forms loginUrl="member_login.aspx"
    defaultUrl="index.aspx" />
</authentication>
Vahid Farahmandian
  • 5,079
  • 5
  • 38
  • 54
Sunil
  • 18,294
  • 25
  • 99
  • 171