【问题标题】:How to mark some files not browsable in IIS如何在 IIS 中标记某些文件不可浏览
【发布时间】:2017-02-06 06:35:54
【问题描述】:

在我的 AngularJS ASP.NET MVC Web API 应用程序中,我有一些 HTML 模板,如下所示:

<form>
    <div class="row">
        <div class="col-xs-8">
            <div class="form-group">
                <label class="col-sm-6 control-label" for="FirstName">First Name:</label>
                <input class="col-sm-6 form-control" id="FirstName" name="FirstName" type="text" ng-model="accountModelStep2.FirstName" placeholder="first name" />
            </div>
        </div>
    </div>
    ...And so on...
    ...Rest of the html omitted. 
</form>

问题是当我启动应用程序并浏览说:

http://localhost:12405/Templates/RegisterStep2.html

IIS 实际上将模板作为 HTML 发送并在浏览器中呈现:

我不希望这些文件可以直接浏览,即使用户在这样的 URL 中键入,浏览器也应该重定向到主页或至少显示一些他们不应该要求这些文件的错误直接地。但是当用户浏览时:

http://localhost:12405/account/register/step1

它应该被渲染。

我该怎么做?

在 Visual Studio 解决方案中,文件如下所示:

【问题讨论】:

    标签: asp.net iis


    【解决方案1】:

    在 IIS 中,在您的网站上,转到 Request Filtering。

    然后选择Hidden Segments 标签。

    最后:Add Hidden Segment... 指定要隐藏的文件夹。

    IIS 请求过滤 - 隐藏段:

    要允许来自 ajax 调用的请求,请在“请求过滤”URL 选项卡中,Allow URL... 指定要允许的 URL:

    或者直接在你的 web.config 中:

    <system.webServer>
        <security>
            <requestFiltering>
                <hiddenSegments>
                    <add segment="Templates" />
                </hiddenSegments>
                <alwaysAllowedUrls>
                    <add url="/Templates" />
                </alwaysAllowedUrls>
            </requestFiltering>
        </security>
    </system.webServer>
    

    【讨论】:

    • 有没有办法在 IIS Express 中做到这一点?
    • 请看我的更新。您可以直接在 web.config 中设置隐藏段。然后,如果您使用 IIS Expess 进行调试,您的隐藏段将无法浏览。
    • 问题,当我浏览到localhost:12405/account/register/step1 时,没有呈现html 模板。我希望它们在 angularjs 需要时被渲染。如何做到这一点?
    • 尝试我的最后一次更新:添加允许的 URL 应该适用于 ajax 调用,但目录浏览仍应被隐藏段指令阻止。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多