39

I use MVC4 web application with Web API. I want to create an action filter, and I want to know which user (a logged-in user) made the action. How can I do it?

public class ModelActionLog : ActionFilterAttribute
{
    public override void OnActionExecuting(SHttpActionContext actionContext)
    {
       string username = ??
    }

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
       ??
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
TamarG
  • 3,522
  • 12
  • 44
  • 74

5 Answers5

65

Bit late for an answer but this is best solution if you are using HttpActionContext in your filter You can always use it as mentioned here:-

public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
   if (actionContext.RequestContext.Principal.Identity.IsAuthenticated)
   {
      var userName = actionContext.RequestContext.Principal.Identity.Name;
   }
}
Atul Chaudhary
  • 3,698
  • 1
  • 31
  • 51
46

You can try

public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
           string username = HttpContext.Current.User.Identity.Name;
        }

Check for authenticated user first:

string userName = null;
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
    userName = HttpContext.Current.User.Identity.Name;
}

Try to use

HttpContext.Current.User.Identity.Name

Hope it works for you

Rahul
  • 5,603
  • 6
  • 34
  • 57
  • 1
    I use WebAPI (System.Web.Http.Filters.ActionFilterAttribute), your solution doesn't work – TamarG May 21 '13 at 06:29
  • 1
    if u r using `System.Web.Http.Filters.ActionFilterAttribute` then just use `HttpContext.Current.User.Identity.Name` for getting User Name on `Action Filter`..see my updated answer,and try atleast once. – Rahul May 21 '13 at 06:43
  • 10
    Please do not suggest to use HttpContext.Current in WebApi. WebApi is async and in case two request come together, you can mix two request data together. It migth seem to work, but it is definitly not thread safe – Lukas K May 16 '14 at 14:04
  • According to this http://stackoverflow.com/a/24973147/90033 if everything is correctly configured and running on/configure for .net 4.5 it should work – Konstantin May 13 '16 at 13:30
  • 1
    It is not Testable because of static HttpContext property. Please use actionContext.RequestContext instead. – Devesh Jul 18 '16 at 10:17
4

Perhaps not the prettiest solution, but for Web API ActionFilter you can do the following:

var controller = (actionContext.ControllerContext.Controller as ApiController);
var principal = controller.User;

Of course, this only applies if your controllers actually inherit from ApiController.

Morten Christiansen
  • 19,002
  • 22
  • 69
  • 94
2

This is what you need

string username = filterContext.HttpContext.User.Identity.Name;
YuriG
  • 368
  • 3
  • 14
  • 1
    I get a message - 'System.Web.Http.Controllers.HttpActionContext' does not contain a definition for 'HttpContext' and no extension method 'HttpContext' accepting a first argument of type 'System.Web.Http.Controllers.HttpActionContext' could be found (are you missing a using directive or an assembly reference?) – TamarG May 21 '13 at 04:05
  • I use System.Web.Http.Filters.ActionFilterAttribute – TamarG May 21 '13 at 04:46
0
 HttpContext.Current.User.Identity.Name
TamarG
  • 3,522
  • 12
  • 44
  • 74