ASP.NET Core 2.1 静态文件与访问授权、防盗链

2018 年 12 月 1 日 DotNet

(给DotNet加星标,提升.Net技能


转自:FlyLolo

cnblogs.com/FlyLolo/p/ASPNETCore2_14.html


我的网站的图片不想被公开浏览、下载、盗链怎么办?


本文主要通过解读一下ASP.NET Core对于静态文件的处理方式的相关源码,来看一下为什么是wwwroot文件夹,如何修改或新增一个静态文件夹,为什么新增的文件夹名字不会被当做controller处理?访问授权怎么做?


一、静态文件夹


所谓静态文件,直观的说就是wwwroot目录下的一些直接提供给访问者的文件,例如css,图片、js文件等。 当然这个wwwroot目录是默认目录,


这个是在Main->CreateDefaultBuilder的时候做了默认设置。


public static class HostingEnvironmentExtensions
{
   public static void Initialize(this IHostingEnvironment hostingEnvironment, string contentRootPath, WebHostOptions options)
   
{
      //省略部分代码
       var webRoot = options.WebRoot;
       if (webRoot == null)
       {
           // Default to /wwwroot if it exists.
           var wwwroot = Path.Combine(hostingEnvironment.ContentRootPath, "wwwroot");
           if (Directory.Exists(wwwroot))
           {
               hostingEnvironment.WebRootPath = wwwroot;
           }
       }
       else
       {
           hostingEnvironment.WebRootPath = Path.Combine(hostingEnvironment.ContentRootPath, webRoot);
       }
      //省略部分代码
   }
}


二、处理方式


前文关于中间件部分说过,在Startup文件中,有一个 app.UseStaticFiles() 方法的调用,这里是将静态文件的处理中间件作为了“处理管道”的一部分,


并且这个中间件是写在 app.UseMvc 之前, 所以当一个请求进来之后, 会先判断是否为静态文件的请求,如果是,则在此做了请求处理,这时候请求会发生短路,不会进入后面的mvc中间件处理步骤。


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   if (env.IsDevelopment())
   {
       app.UseDeveloperExceptionPage();
   }
   else
   {
       app.UseExceptionHandler("/Home/Error");
   }
   app.UseStaticFiles();
   app.UseCookiePolicy();
   app.UseAuthentication();
   app.UseMvc(routes =>
   {
       routes.MapRoute(
           name: "default",
           template: "{controller=Home}/{action=Index}/{id?}");
   });
}


三、新增静态文件目录


除了这个默认的wwwroot目录,需要新增一个目录来作为静态文件的目录,可以Startup文件的 app.UseStaticFiles() 下面继续use,例如下面代码


app.UseFileServer(new FileServerOptions
{
        FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "NewFilesPath")),
        RequestPath = "/NewFiles"
});


含义就是指定应用程序目录中的一个名为“NewFilesPath”的文件夹,将它也设置问静态文件目录, 而这个目录的访问路径为"/NewFiles"。


例如文件夹"NewFilesPath"下面有一个test.jpg, 那么我们可以通过这样的地址来访问它:http://localhost:64237/NewFiles/test.jpg。


四、中间件的处理方式


静态文件的处理中间件为StaticFileMiddleware,主要的处理方法 Invoke 代码如下


public async Task Invoke(HttpContext context)
{
   var fileContext = new StaticFileContext(context, _options, _matchUrl, _logger, _fileProvider, _contentTypeProvider);
   if (!fileContext.ValidateMethod())
   {
      _logger.LogRequestMethodNotSupported(context.Request.Method);
   }
   else if (!fileContext.ValidatePath())
   
{
       _logger.LogPathMismatch(fileContext.SubPath);
   }
   else if (!fileContext.LookupContentType())
   
{
       _logger.LogFileTypeNotSupported(fileContext.SubPath);
   }
   else if (!fileContext.LookupFileInfo())
   
{
       _logger.LogFileNotFound(fileContext.SubPath);
   }
   else
   {
       // If we get here, we can try to serve the file
       fileContext.ComprehendRequestHeaders();
       switch (fileContext.GetPreconditionState())
       {
           case StaticFileContext.PreconditionState.Unspecified:
           case StaticFileContext.PreconditionState.ShouldProcess:
               if (fileContext.IsHeadMethod)
               {
                   await fileContext.SendStatusAsync(Constants.Status200Ok);
                   return;
               }
               try
               {
                   if (fileContext.IsRangeRequest)
                   {
                       await fileContext.SendRangeAsync();
                       return;
                   }
                   await fileContext.SendAsync();
                   _logger.LogFileServed(fileContext.SubPath, fileContext.PhysicalPath);
                   return;
               }
               catch (FileNotFoundException)
               {
                   context.Response.Clear();
               }
               break;
           case StaticFileContext.PreconditionState.NotModified:
               _logger.LogPathNotModified(fileContext.SubPath);
               await fileContext.SendStatusAsync(Constants.Status304NotModified);
               return;
           case StaticFileContext.PreconditionState.PreconditionFailed:                _logger.LogPreconditionFailed(fileContext.SubPath);
               await fileContext.SendStatusAsync(Constants.Status412PreconditionFailed);
               return;
           default:
               var exception = new NotImplementedException(fileContext.GetPreconditionState().ToString());
               Debug.Fail(exception.ToString());
               throw exception;
       }
   }
   await _next(context);
}


当HttpContext进入此中间件后会尝试封装成StaticFileContext, 然后对其逐步判断,例如请求的URL是否与设置的静态目录一致, 判断文件是否存在,判断文件类型等,


若符合要求 ,会进一步判断文件是否有修改等。


五、静态文件的授权管理


默认情况下,静态文件是不需要授权,可以公开访问的。


因为即使采用了授权, app.UseAuthentication(); 一般也是写在 app.UseStaticFiles() 后面的,那么如果我们想对其进行授权管理,首先想到可以改写 StaticFileMiddleware 这个中间件,


在其中添加一些自定义的判断条件,但貌似不够友好。而且这里只能做一些大类的判断,比如请求的IP地址是否在允许范围内这样的还行,如果要根据登录用户的权限来判断(比如用户只能看到自己上传的图片)就不行了,


因为权限的判断写在这个中间件之后。所以可以通过Filter的方式来处理,首先可以在应用目录中新建一个"images"文件夹, 而这时就不要把它设置为静态文件目录了,这样这个"images"目录的文件默认情况下是不允许访问的,


然后通过Controller返回文件的方式来处理请求,如下代码所示


[Route("api/[controller]")]
[AuthorizeFilter]
public class FileController : Controller
{
   [HttpGet("{name}")]
   public FileResult Get(string name)
   
{
       var file = Path.Combine(Directory.GetCurrentDirectory(), "images", name);
       return PhysicalFile(file, "application/octet-stream");
   }
}


在AuthorizeFilter中进行相关判断,代码如下


public class AuthorizeFilter: ActionFilterAttribute
{
   public override void OnActionExecuting(ActionExecutingContext context)
   
{
       base.OnActionExecuting(context);
       if (context.RouteData.Values["controller"].ToString().ToLower().Equals("file"))
       {
           bool isAllow = false;//在此进行一系列访问权限验证,如果失败,返回一个默认图片,例如logo或不允许访问的提示图片
           if (!isAllow)
           {
               var file = Path.Combine(Directory.GetCurrentDirectory(), "images", "default.png");
               context.Result = new PhysicalFileResult(file, "application/octet-stream");
           }
       }
   }
}


推荐阅读

(点击标题可跳转阅读)

聊聊C#中的泛型的使用

.NET Core 中正确使用 HttpClient 的姿势

通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span


看完本文有收获?请转发分享给更多人

关注「DotNet」加星标,提升.Net技能 

登录查看更多
2

相关内容

ASP.NET 是一项微软的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术。 指 Active Server Pages(动态服务器页面),运行于 IIS 之中的程序 。
【2020新书】使用高级C# 提升你的编程技能,412页pdf
专知会员服务
56+阅读 · 2020年6月26日
Python导论,476页pdf,现代Python计算
专知会员服务
254+阅读 · 2020年5月17日
【实用书】Python爬虫Web抓取数据,第二版,306页pdf
专知会员服务
115+阅读 · 2020年5月10日
Python计算导论,560页pdf,Introduction to Computing Using Python
专知会员服务
70+阅读 · 2020年5月5日
【新书】Java企业微服务,Enterprise Java Microservices,272页pdf
【干货】大数据入门指南:Hadoop、Hive、Spark、 Storm等
专知会员服务
94+阅读 · 2019年12月4日
MIT新书《强化学习与最优控制》
专知会员服务
270+阅读 · 2019年10月9日
Cayley图数据库的可视化(Visualize)
Python开发者
5+阅读 · 2019年9月9日
PC微信逆向:两种姿势教你解密数据库文件
黑客技术与网络安全
16+阅读 · 2019年8月30日
用Now轻松部署无服务器Node应用程序
前端之巅
16+阅读 · 2019年6月19日
Kali Linux 渗透测试:密码攻击
计算机与网络安全
15+阅读 · 2019年5月13日
React Native 分包哪家强?看这文就够了!
程序人生
12+阅读 · 2019年1月16日
一天精通无人中级篇:遥控器协议 S-BUS
无人机
48+阅读 · 2018年12月20日
Neo4j 和图数据库起步
Linux中国
8+阅读 · 2017年12月20日
Arxiv
4+阅读 · 2019年8月7日
3D Face Modeling from Diverse Raw Scan Data
Arxiv
5+阅读 · 2019年2月13日
Learning Implicit Fields for Generative Shape Modeling
Arxiv
10+阅读 · 2018年12月6日
Arxiv
7+阅读 · 2018年4月11日
VIP会员
相关VIP内容
相关资讯
Cayley图数据库的可视化(Visualize)
Python开发者
5+阅读 · 2019年9月9日
PC微信逆向:两种姿势教你解密数据库文件
黑客技术与网络安全
16+阅读 · 2019年8月30日
用Now轻松部署无服务器Node应用程序
前端之巅
16+阅读 · 2019年6月19日
Kali Linux 渗透测试:密码攻击
计算机与网络安全
15+阅读 · 2019年5月13日
React Native 分包哪家强?看这文就够了!
程序人生
12+阅读 · 2019年1月16日
一天精通无人中级篇:遥控器协议 S-BUS
无人机
48+阅读 · 2018年12月20日
Neo4j 和图数据库起步
Linux中国
8+阅读 · 2017年12月20日
Top
微信扫码咨询专知VIP会员