c# - How to get dependency injection working with IAsyncAuthorizationFilter in ASP.NET Core -
i have implemented iasyncauthorizationfilter
interface class; in addition, class derived attribute
, can mark controller classes , action methods using it. works far; task onauthorizationasync(authorizationfiltercontext context)
method called before action method, , if request not authenticated, http 401
returned response.
[attributeusage(attributetargets.class | attributetargets.method)] public sealed class customauthenticationattribute : attribute, iasyncauthorizationfilter { public async task onauthorizationasync(authorizationfiltercontext context) { ... } }
this attribute used follows...
[customauthenticationattribute] public class somedatacontroller : controller { [httpget] public async task getdata() { ... } }
now, want use application service (which obtains secret private key information required authentication database) , tried use property injection that. injecting dependencies via ctor not choice here since it´s implemented attribute. tried...
[attributeusage(attributetargets.class | attributetargets.method)] public sealed class customauthenticationattribute : attribute, iasyncauthorizationfilter { public iprivatekeylookupservice keyservice { get; set; } public async task onauthorizationasync(authorizationfiltercontext context) { string publickey = ... ... var privatekey = await this.keyservice.getprivatekeyfrom(publickey); ... } }
...but property injection not seem work here. service registered ioc, properties not wired. asp.net core 1.1 project in use autofac
. in startup
class configureservices
method has that...
public void configureservices(iservicescollection collection) { ... var containerbuilder = new containerbuilder(); containerbuilder.registertype<authkeyservice>().as<iprivatekeylookupservice>(); containerbuilder.populate(services); this.container = containerbuilder.build(); }
does autofac
support auto-wiring iasyncauthorizationfilter
types? , if not, how polyfill functionality?
it turned out simple...
in asp.net core
there typefilter
attribute filter creates filter (specified type
) , satisfies constructor arguments involving dependency injection.
i changed implementation of iasyncauthorizationfilter
; removed properties , added ctor instead, because typefilter
ctor injection work now...
public sealed class customauthenticationattribute : iasyncauthorizationfilter { private readonly iprivatekeylookupservice keyservice; public customauthenticationattribute( iprivatekeylookupservice keyservice) { this.keyservice = keyservice; } public async task onauthorizationasync(authorizationfiltercontext context) { ... } }
i removed inheritance attribute
because it´s not needed anymore declaration of attributeusage
attribute.
so, can use attribute follows...
[typefilter(typeof(customauthenticationattribute))] public class somedatacontroller : controller { [httpget] public async task getdata() { ... } }
passing additional arguments filter´s ctor possible via object[] arguments
property of typefilter
class.
Comments
Post a Comment