Answered by:
Identity - access current logged in user properties

Question
-
User-570626059 posted
I need to access the current logged in custom user field.
How do I reference the current user?
Tuesday, July 3, 2018 8:01 PM
Answers
-
User475983607 posted
Hi, I attempted this on the post above...
i am getting null exception on this line
UpdateUserSetLiveAsync().Wait();
The code looks nothing like the linked tutorial. Is the page directive set to Async=true. Where the Async callback registered as illustrated in the linked tutorial?
ANyway, maybe just forget about the Async stuff and just go with the synchronous method.
manager.Update()
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 4, 2018 1:57 PM
All replies
-
User-570626059 posted
Would this be ok
if (!IsPostBack) { var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); var userrole = Context.GetOwinContext().Authentication.User.IsInRole("Admin"); //get id var finduserid = Context.User.Identity.GetUserId(); //get user var applicationUser = manager.FindById(finduserid); //check if first login i.e Y string isFirstLogin = applicationUser.firstLogin.Trim().ToUpper(); if (isFirstLogin=="Y") { Response.Redirect("~/account/changepassword.aspx"); } }
Tuesday, July 3, 2018 8:09 PM -
User-570626059 posted
also if I want to change the value of a customer property for the current user how do I do it.
var user = manager.FindById(User.Identity.GetUserId()); user.firstLogin = "N";
I need to set firstlogin="N" but then how do i save it or will it do it automatically as above?
Tuesday, July 3, 2018 8:17 PM -
User475983607 posted
The ASP Identity API syntax is...
await manager.UpdateAsync(user);
https://msdn.microsoft.com/en-us/library/dn497586(v=vs.108).aspx
Tuesday, July 3, 2018 8:23 PM -
User1724605321 posted
Hi skyblue ,
Yes . Since "firstLogin " is a custom field , you can set a default value , for example ,1 .When the new user log in make check if this user have field firstLogin = 1. If the user should change password, redirect him to page ChangePasswordPage in other case redirect to your default page. When he fill new password and click Change Password Button, set firstLogin flag in user table to 0 with update function as @mgebhard shows and redirect him to your default page . If you want to know more about how to customize Users and Roles in asp.net identity , please refer to below link :
http://johnatten.com/2014/06/22/asp-net-identity-2-0-customizing-users-and-roles/
Best Regards,
Nan Yu
Wednesday, July 4, 2018 5:19 AM -
User-570626059 posted
Hello I tried using the
await manager.UpdateAsync(user);
however it is not working. It is saying the await operator can only be used within an async method. This is on a button click when user clicks change password.
any ideas?
Wednesday, July 4, 2018 7:15 AM -
User-570626059 posted
Ok I have this working on this particular page, but I am now trying to set the webstatus ( again another customer property) from "Invited to portal" to "Live". So when the user first logs in their account is now LIVE.
I have set the await async as per the code below but when loggin in I now get System.Threading.ThreadAbortException: Thread was being aborted. on the page redirect.
protected async void LogInAsync(object sender, EventArgs e) { if (IsValid) { // 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(textUsername.Value, textPassword.Value, false, shouldLockout: false); switch (result) { case SignInStatus.Success: var finduserid = manager.FindByName(textUsername.Value).Id; var isadmin = manager.IsInRole(finduserid, "Admin"); var currentUser = manager.FindById(finduserid); if (isadmin) { Response.Redirect("~/pages/admin/adminland.aspx"); } else { // check for first login var empty = UtilsString.IsEmpty(currentUser.firstLogin.Trim().ToUpper()); if (!empty) { if (currentUser.firstLogin.Trim().ToUpper() == "Y") { //also set webstatus to now live from invited to portal var user = manager.FindById(User.Identity.GetUserId()); user.webStatus = "Live"; await manager.UpdateAsync(user); Response.Redirect("~/account/managepassword.aspx",false); } else { Response.Redirect("~/pages/userland.aspx",false); } } } 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"] ), true); break; case SignInStatus.Failure: default: dvMessage.InnerText = "Invalid Username and/or Password"; dvMessage.Visible = true; break; } } }
I have got around this by using
Response.Redirect("~/account/managepassword.aspx",false);
is this the correct way of doing it? What is the error caused by? does it just been the update operation is still in progress and I am trying to redirect thus ending the update operation prematurely?
Wednesday, July 4, 2018 11:26 AM -
User475983607 posted
This is a common exception when redirecting in the middle of any request in ASP,NET Web Forms.
By the way, async void is not good and should be async Task. I would move the logic to a new method and have the event handler call the new method. Invoke the redirect in the event handler. See the docs for proper syntax and design patterns.
You might consider going with MVC or ASP Core Razor Pages as async programming has a steep learning curve but easier to handle in MVC and MVC Razor Pages.
Wednesday, July 4, 2018 11:53 AM -
User-570626059 posted
Is this correct
private async Task UpdateUserSetLiveAsync() { var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); var user = manager.FindById(User.Identity.GetUserId()); user.webStatus = "Live"; await manager.UpdateAsync(user); }
and calling by
UpdateUserSetLiveAsync().Wait();
Wednesday, July 4, 2018 1:11 PM -
User475983607 posted
OK so how do I do this correctly.
Did you read the previously linked reference, Using Asynchronous Methods in ASP.NET 4.5?
Do i put it in a separate async task and call this task?Create an async method accomplished the task. Invoke the async method from an event handler using the syntax illustrated in the link.
please help me do this correctly and show how i correctly call the task after login.
Web Forms is an event driven framework. Call the async method from the, I guess, login event. Whatever that might be in your design, maybe a button click.
Wednesday, July 4, 2018 1:30 PM -
User-570626059 posted
Hi, I attempted this on the post above...
is this because I am trying to update the current user properties but the page has already redirected so its returning null for user?
if so how would i get around this?
sorry for all the questions, totally new to thisi am getting null exception on this line
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 85: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); Line 86: var user = manager.FindById(User.Identity.GetUserId()); Line 87: user.webStatus = "Live"; Line 88: await manager.UpdateAsync(user); Line 89:
Wednesday, July 4, 2018 1:32 PM -
User475983607 posted
Hi, I attempted this on the post above...
i am getting null exception on this line
UpdateUserSetLiveAsync().Wait();
The code looks nothing like the linked tutorial. Is the page directive set to Async=true. Where the Async callback registered as illustrated in the linked tutorial?
ANyway, maybe just forget about the Async stuff and just go with the synchronous method.
manager.Update()
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 4, 2018 1:57 PM