Dawid Ruchniewicz

Dawid Ruchniewicz

  • NA
  • 28
  • 3.1k

Identity Framework, access to sign in user.

Nov 19 2018 7:55 AM
Hey, I have question, I configured default Identity in my web app in asp net.core 2.1, and I need details of current sign in user. How can do that???
 
In controller I can do : 
private readonly UserManager<ApplicationUser> _userManager;
ApplicationUser x = await _userManager.GetUserAsync(User);
x.SomeFieldsOrMethods
 
but I need access to these fields in other .cs file where I have a SignalR Hub functions and there I haven't access to the 'User'. 
 
In other way I tried :
After each sign in I add two new claims 
await UserManager.AddClaimAsync(user, new Claim("UserLogin", user.Login));
await UserManager.AddClaimAsync(user, new Claim("UserId", user.Id.ToString()));
 
 then in my Hub.cs file : 
var httpcontext = Context.GetHttpContext();  
string login = httpcontext.User.FindFirst("UserLogin").Value; <--this get a login well
string login = httpcontext.User.FindFirst("UserId").Value; <-- this get an UserId, but in string type, but iny my database I have integer Id. I tried parse to int in few ways but always failed. 
Please help me, how can I get user id (int) and other fields in other .cs files.
Thanks 
 

Answers (6)