User.Identity.GetUserId() null after login again

515 Views Asked by At

i have a problem: when i login User.Identity.GetUserId() return correct value of ID, but when i click logout and then login again with another user User.Identity.GetUserId() return null:

my code:

 protected void LogIn(object sender, EventArgs e)
    {
        string url="";
        if (IsValid)
        {
            // Convalidare la password utente
            var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();



            var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
            FormsAuthentication.RedirectFromLoginPage(User.Identity.GetUserId(), false);


            var roles = manager.GetRoles(User.Identity.GetUserId().ToString());
            Session["Role"] = roles[0];
          switch (Convert.ToString(Session["Role"]))
          { 
              case "GruppoAzienda":
                  url = "G_Aziende/g_aziende.aspx";break;
              case "Azienda":
                  url = "Aziende/Dipendenti.aspx"; break;
          }


            switch (result)
            {
                case SignInStatus.Success:
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response, url);
                    break;
                case SignInStatus.LockedOut:
                    Response.Redirect("/Account/Lockout");
                    break;
                case SignInStatus.RequiresVerification:
                    Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}", 
                                                    Request.QueryString["ReturnUrl"],
                                                    RememberMe.Checked),
                                      true);
                    break;
                case SignInStatus.Failure:
                default:
                    FailureText.Text = "Tentativo di accesso non valido";
                    ErrorMessage.Visible = true;
                    break;
            }
        }
    }

this is logout code:

   protected void Unnamed_LoggingOut(object sender, LoginCancelEventArgs e)
    {
        Context.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        Session.Abandon();


    }

Can someone help?

0

There are 0 best solutions below