Click here to Skip to main content
15,901,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
}


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

What I have tried:

I have tried to use null conditional operator to bypass the error, but it did not work.
Posted
Comments
Maciej Los 9-May-24 17:05pm    
Have you added a reference to Microsoft.AspNet.Identity?
Mekadosky 9-May-24 18:04pm    
Yes I had it added before now and nothing happens.
Graeme_Grant 10-May-24 5:02am    
Set a breakpoint here to check what is returned:
var result = signinManager. ...
Mekadosky 14-May-24 8:58am    
I resolve it by replacing
string userId = User.Identity.GetUserId();

with

string userId = signinManager.AuthenticationManager.AuthenticationResponseGrant.Identity.GetUserId();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900