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
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
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.