c# - Serving files with authorization in an Owin self hosted application -
i have self hosted application using owin , no asp.mvc of type, there no web.config in application.
i have enabled cookie authentication , own authorization provider mechanism works fine. application serves static contents using the next code:
appbuilder.usefileserver(new fileserveroptions() { requestpath = new pathstring("/images"), filesystem = new physicalfilesystem(@"./images"), });
but content not protected owin authentication, easiest way protect files?
*ideally not having implement whole file serving myself.
so far i've managed in way:
var contentfileserver = new fileserveroptions() { requestpath = new pathstring("/content"), filesystem = new physicalfilesystem(@"./content"), }; contentfileserver.staticfileoptions.onprepareresponse = (context) => { if (context.owincontext.authentication.user == null) { // reply unauthorized context.owincontext.response.statuscode = 401; } }; appbuilder.usefileserver(contentfileserver);
looks reasonable way of doing it.
Comments
Post a Comment