我已经阅读了之前关于使用RequireHttpsAttribute保护单个控制器的帖子:
ASP.NET MVC RequireHttps in Production Only
但是有没有一种方法可以应用到整个网站上呢?由于我的主机(discountasp.net),我不能使用"RequireSSL IIS“设置。
发布于 2013-04-11 00:07:48
将RequireHttpsAttribute
注册为全局筛选器。
在global.asax中:
protected void Application_Start()
{
GlobalFilters.Filters.Add(new RequireHttpsAttribute());
//... other stuff
}
发布于 2010-07-20 05:19:03
您始终可以在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);
}
}
发布于 2017-04-15 21:31:31
在您的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。
https://stackoverflow.com/questions/3285014
复制相似问题