security - ASP.NET: First Chance to add something to HttpContext -
we using httpcontext.items save our own securitycontext.
public static isecuritycontext current { { if (!(httpcontext.current.items["securitycontext"] isecuritycontext)) { httpcontext.current.items["securitycontext"] = new securitycontext(...); } return httpcontext.current.items["securitycontext"] isecuritycontext; } }
now problem is, create securitycontext , add httpcontext. when looking @ mvc-lifecycle guess iauthenticationfilter
first chance that. idea or there better "place" add securitycontext httpcontext?
looks want sort of service locator. ioc might option used dependency inversion style of building controllers. in way don't have bother when httpcontext
ready use.
to answer question. in global.asax
use application_prerequesthandlerexecute
method instantiate , store service implementation.
protected void application_beginrequest(object sender, eventargs e) { var context = (sender httpapplication).context; context.items.add("iservice", new service()); }
because method executed before controller created, implementation available. can create service locator class retrieves instance.
public class servicelocator { public static iservice current { { return httpcontext.current.items["iservice"] iservice; } } }
and use class in controller.
public class homecontroller : controller { public actionresult index() { viewbag.message = servicelocator.current.now.tostring(); return view(); } .........
Comments
Post a Comment