29

The only override I see exposed on MVC's AuthorizeAttribute is public override void OnAuthorization( AuthorizationContext filterContext ) which is not suitable for use with async/await because it doesn't return a Task. Is there another way to create an AuthorizeAttribute in MVC that allows the use of async/await?

5
  • You could always create your own attribute. Why do you even need that?
    – Santhos
    May 15, 2014 at 22:22
  • 1
    I second Santhos' question. Why do you need that? May 15, 2014 at 22:30
  • I'm somewhat confused by how you intend to use this async attribute. OnAuthorization is called by the framework during the pipeline processing. It's not called by your own code. So what exactly would call this? May 15, 2014 at 23:34
  • Stijn, like Stephen says, we implemented these features in MVC vNext. May 16, 2014 at 3:59
  • 1
    I needed it to be able to make an async WebRequest to another service after my authorization check passes to backfill some user information.
    – Evan
    May 16, 2014 at 12:30

3 Answers 3

27

ASP.NET MVC today does not support asynchronous filters. Please vote.

However, ASP.NET "vNext", announced at TechEd this week, will support asynchronous filters for MVC.

2
  • 1
    Is there a safe way to call the async method and wait for it to complete? I know there are deadlock concerns with just calling Wait().
    – Evan
    May 16, 2014 at 2:00
  • 5
    No. Wait/Result can cause deadlocks; nested message loops can fail when ASP.NET types assume an ASP.NET sync context; pushing to the background thread and waiting loses the request context. There's no good solution today; the cleanest solution is to use synchronous APIs. :( If you must use async APIs in a filter, you just have to find one solution that works for your particular code. May 16, 2014 at 2:30
5

With asp.net core being released, Stephen Cleary's answer is correct and the ideal way to go if you are running the latest asp.net core.

For those that haven't updated yet, I was able to work around my issue using an async HttpModule that passes state into the AuthorizationFilter via HttpContext.Items. I added more detail about my solution here - http://evandontje.com/2017/08/15/solutions-for-async-await-in-mvc-action-filters/

5

For those who do not yet have the joy of being on .NET core, you can use this method from ASP.NET Web API 2 :

OnAuthorizationAsync override :

public override async Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellation)

for example, you can call an async webapi service like this :

 public override async Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellation)
    {
        await base.OnAuthorizationAsync(actionContext, cancellation);

        if (await IsUserAdminAsync()) /* call any async service */
            return;
        this.HandleUnauthorizedRequest(actionContext);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.