【问题标题】:ASP.NET MVC HTTPS Redirect without exposing authentication informationASP.NET MVC HTTPS 重定向而不暴露身份验证信息
【发布时间】:2015-02-06 03:03:24
【问题描述】:

我有一个网站,我必须在任何地方强制使用 HTTPS。

我担心在进行 HTTP 到 HTTPS 重定向时,如果用户已经登录(通过 cookie 或令牌),则该身份验证信息可能会在不经意间以明文形式传输。

如何确保不会发生这种情况?

【问题讨论】:

    标签: asp.net asp.net-mvc https


    【解决方案1】:

    在不捕获安全信息的情况下安全地进行重定向的一种方法是使用重写引擎 - 假设您在 URL 本身中没有令牌/安全信息(即:会话等)。

    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="HTTPS Redirect" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
    

    如果您在 Fiddler 中分析此配置的结果,您会注意到即使用户经过身份验证,初始的非安全请求也没有安全信息。

    这是 GET 请求:

    GET http://www.someurl.net/Dashboard HTTP/1.1
    User-Agent: Fiddler
    Host: www.someurl.net
    

    响应只是一个标准的 301 永久重定向,没有任何安全传输:

    HTTP/1.1 301 Moved Permanently
    Content-Type: text/html; charset=UTF-8
    Date: Mon, 08 Dec 2014 13:07:53 GMT
    Location: https://www.someurl.net/Dashboard
    Server: Microsoft-IIS/8.0
    X-Powered-By: ASP.NET
    Content-Length: 162
    Connection: keep-alive
    
    <head><title>Document Moved</title></head>
    <body><h1>Object Moved</h1>This document may be found <a HREF="https://www.someurl.net/Dashboard">here</a></body>
    

    【讨论】:

    • HTTP 标头怎么样?
    • 另外,如果不依赖 rewrite 模块,如何做到这一点?
    • 您可能会检查 global.asax 的 Application_BeginRequest 事件,甚至可能创建一个 MVC 过滤器。至于 HTTP 标头,GET 请求是我登录后在我的一个开发站点上捕获的完整 GET 请求,然后请求锁定页面。这就是作为不安全请求的一部分发送的所有内容。
    猜你喜欢
    • 2013-02-14
    • 2015-10-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    相关资源
    最近更新 更多