【问题标题】:Owin only serve files in certain folderOwin 仅提供特定文件夹中的文件
【发布时间】:2014-10-21 13:21:32
【问题描述】:

所以我在玩 Owin 和 Katana,我想在我的公共文件夹中提供静态文件。

我有一个包含样式表的内容文件夹和一个脚本文件夹。

我的创业:

    public void Configuration(IAppBuilder app)
    {
 #if DEBUG
        //when things go south
        app.UseErrorPage();
  #endif

        // Remap '/' to '.\public\'.
        // Turns on static files and public files.
        app.UseFileServer(new FileServerOptions()
        {
            RequestPath = PathString.Empty,
            FileSystem = new PhysicalFileSystem(@".\public"),
        });

    }

因此,如果我浏览到 localhost:8861/,我会转到我的公共文件夹中的 index.html 文件。没关系。 但我也可以浏览到我想要阻止的 localhost:8861/Content/style.css。用户需要的所有东西都应该可以在公共文件夹中访问。其余的都应该被阻止。

我怎样才能做到这一点?

【问题讨论】:

    标签: c# owin static-files katana owin-middleware


    【解决方案1】:

    文件服务器配置正确,不允许访问其他文件夹。我已经在测试 OWIN 自托管项目中对其进行了检查,它按预期工作,只能访问公用文件夹。我假设您使用 IIS 来托管您的 OWIN 应用程序(因此您的应用程序不是自托管的)。如果是这样,IIS 静态文件处理程序允许访问静态文件和目录(以及您的内容文件夹)。因此,您可以搜索如何在 IIS 中禁用对静态文件的访问(可以在 web.config 中完成)或如何限制对其中一些文件的访问。

    您可以从网站的配置中删除 StaticFile Handler,但您应该谨慎操作,因为从此时起 IIS 将不再提供静态文件。

    <configuration>
        <system.webServer>
            <handlers>
               <remove name="StaticFile" />
            </handlers>
        </system.webServer>
    </configuration>
    

    【讨论】:

    • 感谢这确实解决了问题。但是,在我的公用文件夹中提供其他文件似乎存在问题。所以 public 包含一个 index.html、style.css 和 script.js。提供了索引,但 style.css 和 script.js 给出了 404.4。有什么想法吗?
    • 奇怪,在我的电脑上一切正常,但我使用自托管应用程序进行测试。您确定您使用下一个网址 yoursite/style.css 而不是 yoursite/public/style.css 访问这些文件吗?这些文件存在于网站的文件夹中,对吧?
    • 您可以尝试的另一件事。将下一个中间件注册为配置中的最后一个: app.Run(context => { return context.Response.WriteAsync(" Wildcard OWIN App"); });在委托中放置断点并尝试从浏览器访问 style.css。此处理程序是通配符中间件,用于处理其他中间件未处理的所有请求。如果您的应用程序将使用断点停止,OWIN 会处理请求但配置不正确,否则 IIS 配置有问题。
    【解决方案2】:

    如果您需要简单的文件处理,可以完全控制您想要提供或不提供哪些文件,您可以使用一些中间件来完全控制。我这样做是因为我希望在开发过程中提供未缓存的文件服务。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading.Tasks;
    using System.Web;
    
    namespace Owin
    {
        using AppFunc = Func<IDictionary<string, object>, Task>;
    
        public static class DynamicFileExtension
        {    
            /// <summary>
            /// ONLY use during development
            /// </summary>
            public static void UseDynamicFiles(this IAppBuilder app, string baseDirectory)
            {
                app.Use(new Func<AppFunc, AppFunc>(next => (async context =>
                {
                    var method = (string) context["owin.RequestMethod"];
                    var requestpath = (string) context["owin.RequestPath"];
                    var scheme = (string) context["owin.RequestScheme"];
                    var response = (Stream) context["owin.ResponseBody"];
                    var responseHeader = (Dictionary<string, string[]>) context["owin.ResponseHeaders"];
    
                    if (method == "GET" && scheme == "http")
                    {
                        var fullpath = baseDirectory + requestpath;
    
                        // block logic...     
    
                        if (File.Exists(fullpath))
                        {
    
                            using (var file = File.OpenRead(fullpath))
                            {
                                await file.CopyToAsync(response);
                            }
    
                            var mime = MimeMapping.GetMimeMapping(fullpath);
    
                            responseHeader.Add("Content-Type", new[] {mime});
    
                            return;
                        }
                    }
    
                    await next.Invoke(context);
                })));
            }
        }
    } 
    

    我不会在生产中使用它,但它对我有用。

    【讨论】:

      猜你喜欢
      • 2015-07-29
      • 2013-03-22
      • 2016-12-11
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 2016-10-02
      • 2016-01-03
      • 1970-01-01
      相关资源
      最近更新 更多