Home > Software engineering >  How to get UserId for logging inside controller?
How to get UserId for logging inside controller?

Time:10-13

I want to logging my AccountController and i don't want use User.Identity.Name, because i think UserName is sensitive information. Can i use User.FindFirstValue(ClaimTypes.NameIdentifier for getting id few times per action and don't get problem with performance?

CodePudding user response:

Can i use User.FindFirstValue(ClaimTypes.NameIdentifier for getting id few times per action and don't get problem with performance?

Yes, you can use it to get the id per action without any performance problem.

But the HttpContext isn't thread-safe. Reading or writing properties of the HttpContext outside of processing a request can result in a NullReferenceException. So, after getting the id, it is better to check whether it is Null or not.

  • Related