Ninject and Asp.Net Web API problems with dependency injection -
i have asp.net web api project side side mvc3 project. both projects ninject configured dependency resolver.
i'm having problems in web api project, since writing own authorization attribute, in order authorize incoming requests api. in attribute, need inject dependencies perform verifications on request.
so how code in attribute looks:
public class customauthorizeattribute : actionfilterattribute { public string roles { get; set; } [inject] public iauthschemaselector _schemaselector { private get; set; } [inject] public iuserservice _userservice { private get; set; } [inject] public iroleverifier _roleverifier { private get; set; } public override void onactionexecuting(httpactioncontext actioncontext) { try { var principal = _schemaselector.authorize(actioncontext.request, roles, _userservice, _roleverifier); actioncontext.request.properties["ms_userprincipal"] = principal; } catch (exception ex) { //create error response proper http status } } } }
_schemaselector decides authorization schema client using request services contained in api (this because i'm planning support different schemas oauth, api key authorization, etc. right i'm using httpbasic). if eveythign goes ok, set principal user, can have access in apicontroller.
everything goes perfect, until make modifications de mvc3 project, web site. example, there feature in web site takes care of changing user password. suppose have initial password 123456. make request api using httpbasic, api responds http status 202.
now change password web site 1234567, perform request api using old password (123456), , responds 202, wrong.
the _userservice object not behaving (and neither other 2 objects injected), when debug action filter, notice whole object in previous state. im guessing wrong on how ninject configured.
this how configured ninject:
in ninjectwebcommon.cs:
globalconfiguration.configuration .serviceresolver .setresolver(t => kernel.tryget(t), t => kernel.getall(t)); in ninject module file:
kernel.bind<iuserservice>().to<userservice>(); //user service part of unit of work kernel.bind<iunitofwork>().to<efunitofwork>().inrequestscope(); kernel.bind<iroleverifier>().to<roleverifier>(); kernel.bind<iauthschemaselector>().to<authschemaselector>(); it's worth mention i'm injecting _userservice actionfilterattribute using property injection pattern, since can't use constructor pattern. dont know if problem.
please help! i'm kind of lost here!
how binding customauthorizeattribute action? global filter or on per action basis? services/etc. store state in memory?
unfortunately there isn't lot of information here debug problem. perhaps post more information how password changing done , responsible sending 202 response, etc?
Comments
Post a Comment