Get current user in ApiController action without passing user ID as parameter

How can we get the current user in a safe ApiController action without passing the username or userId as a parameter?

We assume that it is available because we are in safe operation. Being in safe action means that the user has already authenticated, and the request has its own token. Given that WebApi has allowed the user, there may be a built-in way to access userId without having to pass it as an action parameter.

+56
c # asp.net-web-api asp.net-identity
Feb 07
source share
9 answers

In WebApi 2, you can use RequestContext.Principal from a method on ApiController

+94
Feb 07 '14 at 2:17
source share

You can also access the main ApiController 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(); 
+19
Nov 11 '14 at 14:19
source share

The hint is in the automatically generated Webapi2 account controller

This getter property is defined as

 public string UserIdentity { get { var user = UserManager.FindByName(User.Identity.Name); return user;//user.Email } } 

and in order to get a UserManager - In WebApi2 -do like the Romans (read as AccountController)

 public ApplicationUserManager UserManager { get { return HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); } } 

It must be compatible in IIS and self-service mode.

+11
Oct 18 '15 at 5:50
source share

Karan Bhandari's answer is good, but the AccountController added to the project is likely to be Mvc.Controller . To convert his response for use in modifying ApiController HttpContext.Current.GetOwinContext() to Request.GetOwinContext() and make sure you add the following 2 using :

 using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; 
+4
Feb 02 '16 at 1:18
source share

In .Net Core, use User.Identity.Name to get a username request.

+3
Jan 30 '17 at 6:02
source share

If you use Asp.Identity UseManager, it automatically sets the value

RequestContext.Principal.Identity.GetUserId ()

based on IdentityUser , which is used when creating IdentityDbContext .

If you ever run user user table and bein token authentication owin, please check my answer.

How to get user context during web api calls?

Hope this still helps. :)

+1
01 Sep '16 at 4:11
source share

None of the above suggestions helped me. The following has been done:

 HttpContext.Current.Request.LogonUserIdentity.Name 

I assume there are many scenarios, and this worked for me. My script included an AngularJS interface and a Web API 2 backend application running under IIS. I had to install both applications to run exclusively under Windows Authentication.

No need to transfer any user information. The browser and IIS exchange the registered user credentials, and the web API has access to the user credentials upon request (from IIS, which I assume).

+1
Jan 10 '17 at 5:15
source share
 string userName; string userId; if (HttpContext.Current != null && HttpContext.Current.User != null && HttpContext.Current.User.Identity.Name != null) { userName = HttpContext.Current.User.Identity.Name; userId = HttpContext.Current.User.Identity.GetUserId(); } 

Or based on Darrell Miller's comment, perhaps use this to get the HttpContext first.

 // get httpContext object httpContext; actionContext.Request.Properties.TryGetValue("MS_HttpContext", out httpContext); 

See also:

How to access HTTPContext as part of your web API action

-one
Feb 07
source share

If you use Windows Active Directory, you can add

  using System.DirectoryServices.AccountManagement 

and in the api method, get the full name as such:

var fullName = UserPrincipal.Current.DisplayName;

-2
Jun 30 '16 at 18:10
source share



All Articles