前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >旧话重提:IIS将http强制https访问的正确方法

旧话重提:IIS将http强制https访问的正确方法

作者头像
崔文远TroyCui
发布2020-02-25 12:50:12
4.2K0
发布2020-02-25 12:50:12
举报
文章被收录于专栏:远在上海远在上海

我以前写过《DTcms4/5中使用HttpModule将http访问301重定向到https》,也写过《使用Certify来自动申请并配置Let’s Encrypt免费SSL证书到IIS8》都提到了如何将IIS的http访问强制为https,如果你现在搜索.net强制https访问,或者iis强制https等关键词,会看到很多错误的指导。

常见问题2:要求SSL

比方说开启“ 要求SSL ”,然后用 403 的html(在 C:\inetpub\custerr\目录下,注意语言版本)重定向js代码,这个千万别用了。

代码语言:javascript
复制
<script type="text/javascript">  
    var url=window.location.href;  
    url=url.replace("http:","https:")  
    window.location.replace(url);
</script>

常见问题2:图形化设置IIS的URL重写工具

还有些介绍安装微软IIS的URL重写工具的,讲了半天一堆截图,操作下来因为版本不一样,还不一定成功,你直接按照我的方法,使用Web Platform Installer安装2.0版本:如下图。

然后也不要去IIS的管理器一个个站点去设置了,只需要在Web.config里面的 <system.webServer>节点内增加如下代码即可:

代码语言:javascript
复制
    <rewrite>
      <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
      </rules>
    </rewrite>

如果需要开启HSTS请用以下代码,首次访问不用https,但之后都会强制使用了,所以建议开启!

代码语言:javascript
复制
           <rules>
                <rule name="redirect to HTTPS" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
                    redirectType="Permanent" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
                    <match serverVariable="RESPONSE_Strict_Transport_Security"
                        pattern=".*" />
                    <conditions>
                        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
                    </conditions>
                    <action type="Rewrite" value="max-age=31536000" />
                </rule>
            </outboundRules>

常见问题3:WebApi还在用自己写的filter重定向

这个方法未必不可以,但我不认为最优。我也在用,以下代码的BaseSystemInfo.ForceHttps是我的一个系统参数,可以自行切换,如果没有安装URL重写工具,本地测试可以http。

但有了URL重定向,这个重定向代码就不会执行了。

代码语言:javascript
复制
public override void OnAuthorization(HttpActionContext actionContext)
        {
            //先检查是否:强制https访问
            var request = actionContext.Request;
            if (BaseSystemInfo.ForceHttps && request.RequestUri.Scheme != Uri.UriSchemeHttps)
            {
                var html = "<p>Https is required</p>";
                if (request.Method.Equals(HttpMethod.Get) || request.Method.Equals(HttpMethod.Head))
                {
                    actionContext.Response = request.CreateResponse(HttpStatusCode.Found);
                    actionContext.Response.Content = new StringContent(html, Encoding.UTF8, "text/html");
                    //重定向
                    var httpsNewUri = new UriBuilder(request.RequestUri);
                    httpsNewUri.Scheme = Uri.UriSchemeHttps;
                    httpsNewUri.Port = 443;
                    actionContext.Response.Headers.Location = httpsNewUri.Uri;
                }
                else
                {
                    actionContext.Response = request.CreateResponse(HttpStatusCode.NotFound);
                    actionContext.Response.Content = new StringContent(html, Encoding.UTF8, "text/html");

                    //重定向
                    var httpsNewUri = new UriBuilder(request.RequestUri);
                    httpsNewUri.Scheme = Uri.UriSchemeHttps;
                    httpsNewUri.Port = 443;
                    actionContext.Response.Headers.Location = httpsNewUri.Uri;
                }
            }
        }

好了,看完这篇文字,你不要再去搜索查找可行的IIS强制https的ssl证书访问了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020年2月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
SSL 证书
腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档