ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,303 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?