9

What is the difference between the following two ways of accessing the principle via an AuthorizeAttribute implementation?

Using HttpContext:

protected override bool IsAuthorized(HttpActionContext actionContext)
{
    return HttpContext.Current.User.IsInRole("DemoRole");
}

Using HttpActionContext:

protected override bool IsAuthorized(HttpActionContext actionContext)
{
    return actionContext.RequestContext.Principal.IsInRole("DemoRole");
}
Dave New
  • 38,496
  • 59
  • 215
  • 394
  • `HttpContext` as documented, it encapsulates all HTTP-specific information about an individual HTTP request, where as `HttpActionContext` is only applicable inside the Action. – Isham Mohamed Nov 10 '17 at 06:18

1 Answers1

4

They are the same, which you can prove by including this line in the method:

Debug.Assert(actionContext.RequestContext.Principal == HttpContext.Current.User);

I would personally use the actionContext, since using HttpContext.Current creates a dependency, and makes it harder to e.g. unit test.

Christian Davén
  • 16,713
  • 12
  • 64
  • 77