zoukankan      html  css  js  c++  java
  • Asp.Net Mvc 整站Https

    网站要使用https需要如下几个步骤

    1、申请https证书,现在已经有很多免费的https证书申请了

    2、服务器中安装证书

    3、网站的连接全部改为https连接

    Asp.Net Mvc网站中整站改为Https连接可以使用以下两种方法

    修改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>

    修改Web.config部署全站HTTPS

    插一句题外话,下个版本的ASP.NET据说要取消掉Web.config。
    在IIS 7.0+的版本上,可以使用URL Rename来完成。Url Rename是指当IIS接收到某个请求后,先根据设定的规则进行更改请求地址后,在发送给网站程序。经测试本方法在Azure App Service (原名 Azure Websites)是可以使用的。
    在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>

    仅修改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">,关于如何整合这些内容,请参阅用于 Web 应用程序项目部署的 Web.config 转换语法

    转载自:https://www.cnblogs.com/qin-nz/p/4591687.html

  • 相关阅读:
    C#-----类DateTime的常用方法
    C#-----字节数组(byte[])和字符串相互转换
    maven执行update命令时报org/apache/maven/shared/filtering/MavenFilteringException错误
    关于dubbo服务的xml配置文件报错的问题
    The method getTextContent() is undefined for the type Node
    jetty各个版本对应的jdk版本
    dubbo启动报java.lang.ClassNotFoundException: javassist.ClassPath
    MAC下安装多版本JDK和切换几种方式
    安装第三方jar包的两种方式
    使用 Nexus 搭建私服仓库时我犯的一个小错误
  • 原文地址:https://www.cnblogs.com/ideacore/p/8005721.html
Copyright © 2011-2022 走看看