asp.net web api - Simple injector webapi authorization attribute -
i'm trying create custom authorization-attribute webapi project.
in attribute inject iauthmodule object. have no clue how implement this. i've found solutions on web have not been successful of them.
this have far:
class="lang-c# prettyprint-override">public void configuration(iappbuilder app) { // webapi config httpconfiguration config = new httpconfiguration(); // simpleinjector var container = new simpleinjector.container(); container.register<iauthmodule, coreauthmodule>(); container.registerwebapifilterprovider(config); container.registerwebapicontrollers(config); container.verify(); config.dependencyresolver = new simpleinjectorwebapidependencyresolver(container); // setup oauth configureoauth(app, container.getinstance<iauthmodule>()); webapiconfig.register(config); app.usecors(microsoft.owin.cors.corsoptions.allowall); app.usewebapi(config); }
and attribute:
public class customauthorizationattribute : authorizeattribute { // how can inject here? public iauthmodule authmodule { get; set; } protected override bool isauthorized(httpactioncontext actioncontext) { homecoming false; } }
the simple injector web api integration guide goes more details in injecting dependencies web api filter attributes section. describes need 2 things:
useregisterwebapifilterprovider
extension method allow simple injector build-up web api attributes. register custom ipropertyselectionbehavior
create sure simple injector inject dependencies attribute's properties. so comes downwards adding next registration:
class="lang-cs prettyprint-override">var container = new container(); container.options.propertyselectionbehavior = new importpropertyselectionbehavior(); container.registerwebapifilterprovider(globalconfiguration.configuration);
where importpropertyselectionbehavior
implemented follows:
using system; using system.componentmodel.composition; using system.linq; using system.reflection; using simpleinjector.advanced; class importpropertyselectionbehavior : ipropertyselectionbehavior { public bool selectproperty(type type, propertyinfo prop) { homecoming prop.getcustomattributes(typeof(importattribute)).any(); } }
this custom ipropertyselectionbehavior
enables explicit property injection properties marked system.componentmodel.composition.importattribute
attribute. property marked attribute injected.
do note dependency injection in attributes sub optimal described here , here.
asp.net-web-api dependency-injection web-api simple-injector
No comments:
Post a Comment