zoukankan      html  css  js  c++  java
  • MVC 部署HTTPS网站

    一、项目配置

    什么是全站HTTPS

    全站HTTPS就是指整个网站的所有页面,所有资源全部使用HTTPS链接。
    当用户的某个请求是明文的HTTP时,应该通过HTTP状态码301永久重定向到对应的HTTPS链接。

    为了实现全站HTTPS,可以从下面两种方法中选取一种。

    (1)修改Global.asax.cs

    Global.asax.cs添加如下代码

    #if !DEBUG
                GlobalFilters.Filters.Add(new RequireHttpsAttribute());
    #endif

    这是配置了ASP.NET的程序,可以处理所有的经过ASP.NET处理的请求;但是对于存放在Web服务器上的其他资源文件(即不经过ASP.NET的程序的处理)无效。
    如果有此需求,应该告知IIS服务器不要私自回复用户请求,要求所有请求都必须由ASP.NET程序执行。
    此时,在Web.config下增加下面的内容,注意是根目录的Web.config而不是Views文件夹的Web.config

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    <system.webServer>

    [QIN.WARNING] 所有的请求都会发送给ASP.NET处理,此时应确保程序有能力处理原来属于IIS负责的那部分内容,比如图片。

    (2)修改Web.config部署全站HTTPS

    2.1  修改最外层Web.Config

    在Web.config的configuration节点下,找到或添加system.webServer节点,并在此节点下添加:

        <rewrite>
          <rules>
            <!-- clear 会清除掉已有的rule,如果添加过别的rule,请注意确认-->
            <clear/>
            <rule name="Force HTTPS" enabled="true">
              <match url="(.*)" ignoreCase="false" />
              <conditions>
                <add input="{HTTPS}" pattern="off" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
            </rule>
          </rules>
        </rewrite>

    2.2  仅修改Release的Web.config

    这样就算轻松搞定了,但是在开发时,我们不希望要求HTTPS,这时可以通过只修改Release的Web.config来解决。
    首先,不要修改Web.config,而是找到Web.Release.config(VS点开Web.config的小箭头)。
    configuration节点下添加如下内容:

      <system.webServer>
        <rewrite xdt:Transform="Insert">
          <rules>
            <!-- clear 会清除掉已有的rule,如果添加过别的rule,请注意确认-->
            <clear/>
            <rule name="Force HTTPS" enabled="true">
              <match url="(.*)" ignoreCase="false" />
              <conditions>
                <add input="{HTTPS}" pattern="off" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>

    这个如刚才的区别仅仅在于<rewrite xdt:Transform="Insert">

    二、IIS配置

    1,新建网站,选中类型为

    https,然后更改SSL证书为你配置的SSL证书

    对于SSL证书的配置是这样的

    点开第二步,然后点击

    创建自签名证书

    确定以后点开网站看到有个SSL,

    双击进去,再选中

    要求SSL

    选中此步就是为了防止浏览器认为你的网站不安全阻止网站的访问,到此,证书配置完成

    然后发现IIS无法绑定域名,因为IIS7默认不支持HTTPS绑定域名,此时需要自己手动去设置

    首先打开

    C:Windowssystem32inetsrvconfigapplicationHost.config

    在里面找到

    <bindings>
                        <binding protocol="https" bindingInformation="*:443" />
                        <binding protocol="net.tcp" bindingInformation="808:*" />
                        <binding protocol="net.pipe" bindingInformation="*" />
                        <binding protocol="net.msmq" bindingInformation="localhost" />
                        <binding protocol="msmq.formatname" bindingInformation="localhost" />
                        <binding protocol="http" bindingInformation="*:80:www.console.com" />
                    </bindings>

    找到https的配置项目,修改为:

    <binding protocol="https" bindingInformation="*:443:www.console.com" />

    这里面需要注意的是:bindings节点有多个,需要找到你配置的站点,默认是<binding protocol="https" bindingInformation="*:443" />

    然后保存,此时,HTTPS在IIS7上绑定域名搞定

  • 相关阅读:
    Html代码查看器
    Http请求
    HTTP协议
    AsyncTask2
    AsyncTask
    幽灵线程解决方案
    handler消息机制入门
    多叉树的树形背包常见建模方法
    Codeforces Round #263
    怎样在win7下装ubuntu(硬盘版安装)
  • 原文地址:https://www.cnblogs.com/become/p/8857280.html
Copyright © 2011-2022 走看看