0

I have followed on from the following answer here: How to extend IdentityUser with custom property

My variables I have added (following the above answer) are:

int numberID
string fullName

This is built upon the default Visual Studio 2017 ASP.net Web Application selected with options Web API and Individual User Accounts Authentication.

I am now trying to read the current value of both numberID and fullName of the current user inside the ValuesController of the project.

I have tried doing

var currentUser = System.Web.HttpContext.Current.User.Identity.GetUserId();
var currentnumberID = currentUser.numberID;

But am having no sucess with the last line of code.

I have also used User.Identity.GetUserName() and was wondering if there was a way to access my new variables WITHOUT creating a User Identity Extension?

OR

Better yet, with my Web API app what is the best way to add additional variables / fields for the user. Ie I would like to add both fullName and numberID associated to each user and be able to access these in my Web API controllers (eg call for current user and list the variables associated to the current user). I am beginning to think I have taken the wrong road by trying to use UserIdentity.

EDIT Ended up biting the bullet and following the very easy approach here: How to Get Custom Property Value of the ApplicationUser in the ASP.Net MVC 5 View? of adding a User Identity extension. Previous answers seemed complicated but that answer was simple and works!

TehNewbie
  • 51
  • 2
  • 8

1 Answers1

1

The problem is that you are reading UserId and treating it like User object.

You need to use UserManager, to get the user User object. Inside controller's action method, you have access to UserManager, so you can simply use it:

var currentUser = UserManager.FindById(User.Identity.GetUserId());

If you are not inside controller's action method, then you need to get the UserManager from HttpContext:

var UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
  • I changed the later to "var UserManger = System.Web.HttpContext.GetOwinContext()..." but I am now receiving the error "httpContext does not contain a definition for GetOwinContext()" I have also added "using Microsoft.Owin.Host.SystemWeb" to the top of my file with no success :s – TehNewbie Feb 06 '19 at 04:45
  • Please refer to the [manuals](https://learn.microsoft.com/en-us/aspnet/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project) for adding the packages... you need: you need: Microsoft.AspNet.Identity; Microsoft.AspNet.Identity.Owin; Microsoft.Owin.Security; – Hooman Bahreini Feb 06 '19 at 05:21
  • Why do you need HttpContext.GetOwinContext(), anyways? I thought you said you are doing this inside the controller... UserManager is accessible inside controller. – Hooman Bahreini Feb 06 '19 at 05:23
  • I'm inside another controller called ValuesController, ie: https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect/blob/master/TodoListService/Controllers/ValuesController.cs I simply want to be able to return the current authorized user numberID – TehNewbie Feb 06 '19 at 11:49