Get the current user, within an ApiController action, without passing the userID as a parameter
In WebApi 2 you can use RequestContext.Principal
from within a method on ApiController
You can also access the principal using the User
property on ApiController
.
So the following two statements are basically the same:
string id;id = User.Identity.GetUserId();id = RequestContext.Principal.Identity.GetUserId();
Hint lies in Webapi2 auto generated account controller
Have this property with getter defined as
public string UserIdentity { get { var user = UserManager.FindByName(User.Identity.Name); return user;//user.Email } }
and in order to get UserManager - In WebApi2 -do as Romans (read as AccountController) do
public ApplicationUserManager UserManager { get { return HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); } }
This should be compatible in IIS and self host mode