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:\Windows\system32\inetsrv\config\applicationHost.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上绑定域名搞定

转载于:https://www.cnblogs.com/become/p/8857280.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值