Asp.Net 中 HTTP 和 HTTPS 切换

目的

  • HTTP,超文本传输协议,明文传输,无状态,服务器默认端口80
  • HTTPS,具有SSL加密的HTTP,加密传输,需要申请ca证书,服务器默认端口443

虽然现在推行全站https协议,但是httpshttp更耗费资源,所以一部分网站还是实行部分http,一部分https,本文讲的就是如何在Asp.Net MVC项目中实现httphttps的切换

步骤

1.如果项目需求是全站https的话,只需在控制器前使用特性RequireHttpsAttribute,他的文档为表示一个特性,该特性用于强制通过 HTTPS 重新发送不安全的 HTTP 请求。,就是所有请求都转化为https链接.

2.如果我们要实现部分http,部分https,就要创建一个新的特性,使其继承RequireHttpsAttribute

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Diagnostics;


namespace Https.Controllers.extends
{

    public class SwitchHttpsAttribute : System.Web.Mvc.RequireHttpsAttribute
    {
        /// <summary>
        /// 字段表示是否需要安全的https链接,默认不需要
        /// </summary>
        public bool RequireSecure = false;

        /// <summary>
        /// 重写验证方法,判断是否需要https,如果需要https,就交给父类的方法处理,如果不需要,就自己处理
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
        {
            if (RequireSecure)
            {
                //需要https,执行父类方法,转化为https
                base.OnAuthorization(filterContext);
            }
            else
            {
                //如果设置为非安全链接,即http,进入该区块
                //判断链接,如果为https,这转换为http
                if (filterContext.HttpContext.Request.IsSecureConnection)
                {
                    HandleNonHttpRequest(filterContext);
                }
            }
        }

        /// <summary>
        /// 重写处理链接方法,处理https请求,使其重定向http
        /// </summary>
        /// <param name="filterContext"></param>
        protected virtual void HandleNonHttpRequest(AuthorizationContext filterContext)
        {
            if (String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            {

                // 从web.config中获取https的端口
                string port =":"+ System.Configuration.ConfigurationManager.AppSettings["HttpPort"];

                // redirect to HTTP version of page
                string url = "http://" + filterContext.HttpContext.Request.Url.Host + port+ filterContext.HttpContext.Request.RawUrl;

                //重定向
                filterContext.Result = new RedirectResult(url);
            }
        }

    }
}

2.http请求需要从web.config中获取端口,所以要添加配置,HostName为可选,HttpPost为该项目的端口.

<appSettings>
  <add key="HostName" value="localhost"/>
  <add key="HttpPort" value="8066"/>
</appSettings>

3.接下来可以在控制器中使用新的特性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Https.Controllers.extends;

namespace Https.Controllers
{
    public class HomeController : Controller
    {

        //[RequireHttps]
        [SwitchHttps]
        public ActionResult Index()
        {
            return View();
        }

        //[RequireHttps]
        [SwitchHttps(RequireSecure = true)]
        public ActionResult About()
        {
            return View();
        }
    }
}

4.调试项目,即可验证是否完成对httphttps的控制.

5.发布应用,部署到IIS上的时候一个项目记得要绑定两个域名,分别为httphttps.

参考资料

-超文本传输安全协议

-C# 特性(Attribute)

原文地址:https://www.cnblogs.com/clockwork/p/5722797.html