1

I want to apply an action filter on one of my ActionResult methods.

I want to be able to pass to the ActionFilterAttribute the currently logged in user id.

Is there either a way I can pass the user to the attribute or have the attribute pick up the logged in user?

griegs
  • 22,624
  • 33
  • 128
  • 205
  • 2
    I don't have the time to write out a full answer right now, but you can access the current HttpContext from the ActionFilterAttribute and look at the User parameter of that. If that helps, tell me, and I'll make this into an answer and explain more (with code). :) – Maxim Zaslavsky Jan 20 '11 at 05:11

3 Answers3

1

Note that there are two different ActionFilterAttributes:

System.Web.Http.Filters.ActionFilterAttribute does not have an HttpContext property while System.Web.Mvc.ActionFilterAttribute does

If you are having trouble figuring out how to get the http context from within your filter its probably because you are using the wrong one.

Yaur
  • 7,333
  • 1
  • 25
  • 36
  • Hi Yaur - I *DO* have an MVC ActFiltAtt. How do I get an httpContext off it, please! – Brondahl Nov 27 '14 at 16:01
  • @Brondahl When (for example) `OnActionExecuting` is called, it will be called with an `ActionExecutingContext` that has a property called `HttpContext` which is the HttpContext. – Yaur Dec 01 '14 at 16:07
1

See:

MVC: creating a custom [AuthorizeAttribute] which takes parameters?

Lol, didn't realize I sent you on a double hop, direct link:

How to pass parameters to a custom ActionFilter in ASP.NET MVC 2?

Community
  • 1
  • 1
John Farrell
  • 24,673
  • 10
  • 77
  • 110
1

What about:

HttpContext.Current.User
Julian
  • 33,915
  • 22
  • 119
  • 174