Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User.Identity.IsAuthenticated is false after successful login

I need to get the UserId Guid directly after a successful login. The following code doesn't work:

if (Membership.ValidateUser(txtUsername.Value, txtPassword.Value)) {     FormsAuthentication.SignOut();     FormsAuthentication.SetAuthCookie(txtUsername.Value, true);      if (HttpContext.Current.User.Identity.IsAuthenticated)     {         // doesn't run         Guid puk = (Guid)Membership.GetUser().ProviderUserKey;                 } } 

The following code does work:

if (Membership.ValidateUser(txtUsername.Value, txtPassword.Value)) {     FormsAuthentication.SignOut();     FormsAuthentication.SetAuthCookie(txtUsername.Value, true);      MembershipUser user = Membership.GetUser(txtUsername.Value);      if (user != null)     {         Guid puk = (Guid)user.ProviderUserKey;     } } 

Why does this happen? Is there something more to do besides SetAuthCookie?

like image 764
notAnonymousAnymore Avatar asked Jul 14 '13 19:07

notAnonymousAnymore


People also ask

Why is user identity IsAuthenticated false?

isauthenticated is False when a user is already logged in.

What is IsAuthenticated identity?

Obviously, User. Identity. IsAuthenticated checks to see if the User is authenticated while Request. IsAuthenticated checks to see if the Request is Authenticated.

What is HttpContext user identity?

It just holds the username of the user that is currently logged in. After login successful authentication, the username is automatically stored by login authentication system to "HttpContext.Current.User.Identity.Name" property.

How does request IsAuthenticated work?

Request. IsAuthenticated will then return true . In the case of Forms authentication, the forms authentication module uses the encrypted authentication ticket contained in the authentication cookie to authenticate the user. Once it has done this, it replaces the GenericIdentity in Context.


1 Answers

I had the same problem too. I forgot to set the web.config configuration.

Maybe you missed too.

   <system.web>      <authentication mode="Forms">       <forms loginUrl="~/user/login" timeout="1000" name="__Auth" />     </authentication>     </system.web>  
like image 121
cihancoskun Avatar answered Sep 18 '22 15:09

cihancoskun