Answered by:
Find User ID from the login page

Question
-
User-1664485818 posted
Hi folks, how do you find the user ID from the Login page, any help much appreciated.
At the moment I'm saving the user name to a session, but I really need the User ID, code below;
protected void LogIn(object sender, EventArgs e) { if (IsValid) { Session["UserName"] = UserName.Text; // Validate the user password var manager = new UserManager(); ApplicationUser user = manager.Find(UserName.Text, Password.Text); if (user != null) { IdentityHelper.SignIn(manager, user, RememberMe.Checked); //IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); IdentityHelper.RedirectToReturnUrl("~/Garages/Bookings.aspx", Response); } else { FailureText.Text = "Invalid username or password."; ErrorMessage.Visible = true; Response.Redirect("~/pages/AccessDenied.aspx"); } }
Monday, February 22, 2016 10:01 PM
Answers
-
User614698185 posted
Hi brucey,
You could use HttpContext.User property to get or set security information for the current HTTP request.
You can call User.Identity.GetUserId after you login:
Session["UserName"] = System.Web.HttpContext.Current.User.Identity.GetUserId();
Best Regards,
Candice Zhou
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, February 23, 2016 6:41 AM -
User1124521738 posted
Why on earth, since you are using the auth framework, would you want to put stuff into session that can easily get lost (app pools cycle and lose session, and also, if on a server farm with load balancers, rolling to another server will make you lose the values)? Just use System.Web.HttpContext.Current.User.Identity.Name when you need the username.
In case the username doesn't validate, and you really need the info to be in session, don't set it until after the "if (user != null)" test, else you've assigned session to whatever is in UserName.Text which could be garbage since this is prior to validating the username/password.
Note your key is called username, but you have been talking about user id which is actually something completely different in the new auth framework. If you want the actual User's ID rather than username, again, wait until after you've checked that user != null and then do the assignment: Session["UserId"] = user.Id;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, February 23, 2016 11:44 PM
All replies
-
User1633621018 posted
Hi Brucey,
Are you looking for this?
User.Identity.GetUserId();
Monday, February 22, 2016 10:22 PM -
User-1664485818 posted
Thanks PawanPal, I'm having a wee problem my session is empty, I'm trying to pass the UserID to the session, code below;
protected void LogIn(object sender, EventArgs e) { if (IsValid) { Session["UserName"] = User.Identity.GetUserId();
Monday, February 22, 2016 10:48 PM -
User614698185 posted
Hi brucey,
You could use HttpContext.User property to get or set security information for the current HTTP request.
You can call User.Identity.GetUserId after you login:
Session["UserName"] = System.Web.HttpContext.Current.User.Identity.GetUserId();
Best Regards,
Candice Zhou
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, February 23, 2016 6:41 AM -
User-1664485818 posted
Nope session still empty, any other ideas?
Tuesday, February 23, 2016 10:36 PM -
User1124521738 posted
Why on earth, since you are using the auth framework, would you want to put stuff into session that can easily get lost (app pools cycle and lose session, and also, if on a server farm with load balancers, rolling to another server will make you lose the values)? Just use System.Web.HttpContext.Current.User.Identity.Name when you need the username.
In case the username doesn't validate, and you really need the info to be in session, don't set it until after the "if (user != null)" test, else you've assigned session to whatever is in UserName.Text which could be garbage since this is prior to validating the username/password.
Note your key is called username, but you have been talking about user id which is actually something completely different in the new auth framework. If you want the actual User's ID rather than username, again, wait until after you've checked that user != null and then do the assignment: Session["UserId"] = user.Id;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, February 23, 2016 11:44 PM -
User-1664485818 posted
Thanks ninianner,
I never really thought about "lose session, and also, if on a server farm with load balancers, rolling to another server will make you lose the values"
Point noted..
NB: I have tried to pass the Userid to a session within the login.aspx, for some reason this will not work, the following code works ok in other pages!!
Session["Userid"] = System.Web.HttpContext.Current.User.Identity.GetUserId();
Wednesday, February 24, 2016 9:22 AM