MVC要求整个网站的https

我已经阅读了以前使用RequireHttpsAttribute来保护单个控制器的文章:

仅在生产中使用ASP.NET MVC RequireHttps

但有没有办法将这个应用到整个网站? 由于我的主机(discountasp.net)我不能使用“RequireSSL IIS”设置。

RequireHttpsAttribute注册为全局filter。

在global.asax中:

 protected void Application_Start() { GlobalFilters.Filters.Add(new RequireHttpsAttribute()); //... other stuff } 

我结束了使用IIS URL重写2.0强制网站切换到HTTPS。 这个web.config中的代码可以做到这一点:

  <system.webServer> <!-- This uses URL Rewrite 2.0 to force the entire site into SSL mode --> <rewrite xdt:Transform="Insert"> <rules> <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> 

你可以随时在你的global.asax的应用程序级别添加一个检查

 protected void Application_BeginRequest(Object sender, EventArgs e) { if (!HttpContext.Current.Request.IsSecureConnection) { Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl); } } 

为了使MVC 3及更高版本的这个答案保持最新,请在App_start文件夹的Filterconfig.cs文件中使用以下内容

  filters.Add(new RequireHttpsAttribute()); 

很明显,你将需要你的服务器IISconfiguration使用有效的SSL证书,便宜的证书可以在这里购买: https : //www.namecheap.com/我想我最后一次购买一个,每个域名每年9美元。

这不是使用RequireHttps但我认为这是一个更好的解决scheme,因为它在MVC生命周期中更快地捕获redirect。

 public class RedirectModule : IHttpModule { private HttpApplication _context; public void Init(HttpApplication context) { _context = context; _context.PostResolveRequestCache += HttpRedirect; } public void HttpRedirect(Object src, EventArgs args) { if (_context.Request.Url.Scheme == Uri.UriSchemeHttp) { //Redirect to https var scheme = Uri.UriSchemeHttps + "://"; var authority = _context.Request.Url.Authority; var url = _context.Request.RawUrl; var redirectTo = scheme + authority + url; _context.Response.PermanentRedirect(redirectTo); } } public void Dispose() { } } 

这个想法来自这篇文章 。

您可以在您的Web.configGlobal.asax内注册模块。 我会告诉你在web.cofig。

 <system.webServer> <modules> <add name="ConfigModuleName" type="Your.Namespace.RedirectModule"/> </modules> </system.webServer> 

MVC 6(ASP.NET Core 1.0)在注册filter方面略有不同:

Startup.cs – 带有filter的AddMvc用于RequireHttpsAttribute :

 public void ConfigureServices(IServiceCollection services) { // TODO: Register other services services.AddMvc(options => { options.Filters.Add(typeof(RequireHttpsAttribute)); }); } 

devise决定解释:

  1. 在Startup.cs中使用filter来进行全局设置(因为我们希望它可以在任何地方应用)。 启动应负责注册和设置所有全球规则。 如果您的公司雇用了一名新的开发人员,她希望在Startup.cs中find全局设置。
  2. 由于经过validation(由Microsoft) 使用RequireHttpsAttribute逻辑。 如果可以通过重新使用创build的Microsoft组件来提供相同的逻辑来避免使用“神奇”string,如“http://”和“https://”。

如果你在没有SSL的本地主机上运行你的MVC网站:

  • http :// localhost:1337 /(不SSL)
  • https :// localhost:1337 /(SSL)

考虑如何在本地主机上不使用SSL的情况下运行,同时还需要在生产环境中使用https 。

注意:

作为替代scheme ,我们可以创build一个“BaseController:Controller”,并使所有控制器都从“BaseController”(而不是Controller)inheritance。 然后我们只需要设置属性1的全局位置(并且不需要在Startup.cs中注册filter)。

有些人更喜欢属性风格。

使用示例:

 [RequireHttpsAttribute] public class BaseController : Controller { // Maybe you have other shared controller logic.. } public class HomeController : BaseController { // Add endpoints (GET / POST) for Home controller } 

在你的FilterConfig.cs中应用这个:

 public static void RegisterGlobalFilters(GlobalFilterCollection filters) { // only if Debug is not enabled, do not require https for local development if (!HttpContext.Current.IsDebuggingEnabled) filters.Add(new RequireHttpsAttribute()); //... any other filters } 

这应该强制您的应用程序在每个页面上使用https。

在Global.asax.cs中,使用“RegisterGlobalFilters”注册全局属性。

 public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new RequireHttpsAttribute()); //eg filters.Add(new HandleErrorAttribute()); //eg filters.Add(new System.Web.Mvc.AuthorizeAttribute()); } 

你可以为你的所有控制器使用基类,并用require ssl属性来装饰它。