C# AuthorizeAttribute InstancePerRequest Without Autofac

169 views Asked by At

I try to create new AuthenticationAttribute for per-request. I find a way with IAutofacAuthorizationFilter I can register in WebApiConfig, then I can use new instance for per request. However without autofac is there any way to do that.

public class AuthenticationAttribute : IAutofacAuthorizationFilter
{
    private readonly IAuthService authService;

    public AuthenticationAttribute(IAuthService authService)
    {
        this.authService = authService;
    }

    public  async Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
    {
        //some actionContext control with authService
    }
}

I need to do this because IAuthService is instance per-request. I'm using Autofac for DI but I don't want to use Autofac in my code directly like this.

0

There are 0 answers