User.identity.getuserid() returns null after signin.

Emeka Okoye 46 Reputation points
2024-05-10T02:12:49.11+00:00

I have a piece of code for login in asp.net C# webform. it uses inbuilt login function, and User.Identity.GetUserId() to catch the userId. But after login the User.Identity.GetUserId() returns null. Below is my login code

// Validate the user password
                    var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                    var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
                    // This doen't count login failures towards account lockout
                    // To enable password failures to trigger lockout, change to shouldLockout: true
                    var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
                    switch (result)
                    {
                        case SignInStatus.Success:
                            string userId = User.Identity.GetUserId();
                            var user = (new AppUsersDbContext()).Users.FirstOrDefault(s => s.Id == userId);
                            string SessionUserId = userId;
                            Session["UserId"] = SessionUserId;
                            IdentityHelper.RedirectToReturnUrl("/Dashboard.aspx", Response);
                        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 = "Invalid login attempt";
                        ErrorMessage.Visible = true;
                        break;
                    }
}		


This is my Dashboard page load

protected void Page_Load(object sender, EventArgs e)
        {
            if ((Session["UserId"] != null))
            {
               
                string UId = Session["UserId"].ToString();
}

At the page load of the Dashboard, the Session["UserId"].ToString() returns NULL.

I can't really figure out what is causing the NULL. can anyone help? How do I resolve the issue?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,303 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,361 questions
{count} votes