HTTPS - Mvc3 - Visual Studio 2010
问题描述:
我有一个要求,即网站需要在https模式下打开(本地开发除外)。这是内部应用程序。HTTPS - Mvc3 - Visual Studio 2010
当我运行网站与web.config条目为https为true,它看起来像网站进入循环运动,并重复一次又一次的请求。
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
//make sure that the remote site opens in https mode.
bool isSSL = false;
bool.TryParse(ConfigurationManager.AppSettings[ApplicationKeys.IsSSLRequired], out isSSL);
if (isSSL && !HttpContext.Current.Request.IsLocal && !HttpContext.Current.Request.IsSecureConnection)
filters.Add(new RequireHttpsAttribute());
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
//RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
//wire up Unity IoC
container = new UnityContainer();
UnityBootstrapper.ConfigureContainer(container);
EntityMapper.MapEntities();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
//wire up Unity Controller Factory
ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory());
}
我在这里错过了什么?
答
由于您已经在利用web.config来驱动此功能,因此我建议您使用URL Rewrite。
您可以设置规则将非HTTPS流量重定向到HTTPS。看到这个线程配置:
http://forums.iis.net/t/1149780.aspx
有了到位,可以进一步提高通过利用web.config transformations以启用规则,当你部署到生产环境中的开发经验。
答
在你的Global.asax:
protected void Application_BeginRequest()
{
bool isSSL = false;
bool.TryParse(ConfigurationManager.AppSettings[ApplicationKeys.IsSSLRequired], out isSSL);
if (isSSL && !Context.Request.IsSecureConnection)
Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
}
+0
这导致页面没有正确重定向错误。就像我的一样。 – 2012-03-20 19:53:09
答
只是一个想法。 Application_Start()是否提供HttpContext.Current.Request?
我已经从全局文件中删除了代码,并在web.config中创建了一条规则。当我使用http时,我收到错误消息“页面没有正确重定向”。这里是规则: –
2012-03-20 19:25:12
I” m猜测你处于无限重定向循环中,因为你没有指定重定向到HTTPS。根据您在评论中提供的内容,url属性应该类似于“https://dev.domain.com/Study{R:1}”。 – 2012-03-20 20:29:39
所以它需要是https://dev.domain.com/Study/ {R:1} ??? – 2012-03-20 21:20:19