首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MVC RequireHttps整个站点

MVC RequireHttps整个站点
EN

Stack Overflow用户
提问于 2010-07-20 04:55:38
回答 6查看 39.2K关注 0票数 68

我已经阅读了之前关于使用RequireHttpsAttribute保护单个控制器的帖子:

ASP.NET MVC RequireHttps in Production Only

但是有没有一种方法可以应用到整个网站上呢?由于我的主机(discountasp.net),我不能使用"RequireSSL IIS“设置。

EN

回答 6

Stack Overflow用户

发布于 2013-04-11 00:07:48

RequireHttpsAttribute注册为全局筛选器。

在global.asax中:

protected void Application_Start()
{
    GlobalFilters.Filters.Add(new RequireHttpsAttribute());

    //... other stuff
}
票数 132
EN

Stack Overflow用户

发布于 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);
   }
}
票数 20
EN

Stack Overflow用户

发布于 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。

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3285014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档