首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >远程需要HTTPS 5

远程需要HTTPS 5
EN

Stack Overflow用户
提问于 2014-11-10 17:48:12
回答 3查看 1.4K关注 0票数 1

我有以下属性,以确保以https模式打开远程站点页。

代码语言:javascript
运行
复制
public class RemoteRequireHttpsAttribute : RequireHttpsAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentException("Filter Context");
            }

            if (filterContext != null && filterContext.HttpContext != null)
            {
                if (filterContext.HttpContext.Request.IsLocal)
                {
                    return;
                }
                else
                {
                    string val = ConfigurationManager.AppSettings["RequireSSL"].Trim();
                    bool requireSsl = bool.Parse(val);
                    if (!requireSsl)
                    {
                        return;
                    }
                }
            }

            base.OnAuthorization(filterContext);
        }
    }

本地开发现在正常工作,因为我不希望它在https模式下打开。

Dev站点以https模式打开页面-这里没有问题(单个节点)。

作为生产(负载平衡-2节点)站点,我目前正在设置的是给我以下错误。请注意,dev和prod站点具有相同的设置和web.config。

页面没有正确地重定向 Firefox检测到服务器正在以永远不会完成的方式重定向此地址的请求。 此问题有时可能是由于禁用或拒绝接受cookie造成的。

开发站点url类似于http://dev.datalab.something.org

Prod站点url类似于http://datalab.something.org

这是电话

代码语言:javascript
运行
复制
[RemoteRequireHttps]
public ActionResult Index(string returnUrl, string error)

我在这里错过了什么?

更新1:我的管理员已经确认已经在lad平衡器evel上设置了终端。我看过iis站点设置,没有看到https绑定。我只看到http绑定。他也需要设置https绑定吗?

更新2: @AlexeiLevenkov为我指明了正确的方向,这个职位有我使用的代码,它正在工作。将代码移到一个单独的答案中。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-11-10 23:03:48

您的站点落后于执行SSL终止的负载均衡器-因此,无论用户看到什么,站点的所有传入流量都是HTTP。这会导致您的代码总是尝试重定向到HTTPS版本,从而导致无限循环。

解决以下问题的办法:

  • 通常,完成SSL终端的负载平衡器将通过自定义报头转发原始IP/协议。X转发-protox-forwarded-for是用于此目的的常用方法。您可能需要与网络管理员检查是否需要使用这些头或其他配置。
  • 或者,您可以关闭SSL终端,但它会给服务器带来额外的负载。
  • 还可以配置负载均衡器,以便与与传入请求相同的协议的服务器进行通信。

如何调查这一问题:

  • 查看http调试器(如Fiddler),看看是否在循环中获得30倍重定向请求。如果没有重定向-可能的代码是错误的。
  • 如果您看到重复重定向,很可能意味着站点没有看到实际的请求信息--可能是协议、路径cookie丢失。
  • 要继续调查,请查看用户和服务器之间的设备(CDN、代理、负载均衡器,.)--每个设备都有很好的机会松散一些日期或转换协议。
票数 2
EN

Stack Overflow用户

发布于 2014-11-10 17:54:43

并不是说我反对编写好的自定义属性,也许在web.config中执行重定向并使用web.config可用的转换将下面的启用值从false更改为true对于生产部署是否有意义?

代码语言:javascript
运行
复制
<rewrite>
  <rules>
    <rule name="SSL_ENABLED" enabled="false" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
票数 1
EN

Stack Overflow用户

发布于 2014-11-10 23:24:55

如@AlexeiLevenkov所指出的,将修复移到另一个答案中。

代码语言:javascript
运行
复制
public class RemoteRequireHttpsAttribute : RequireHttpsAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentException("Filter Context");
            }

            if(filterContext.HttpContext != null)
            {
                if (filterContext.HttpContext.Request.IsSecureConnection)
                {
                    return;
                }

                var currentUrl = filterContext.HttpContext.Request.Url;
                if (currentUrl.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.CurrentCultureIgnoreCase))
                {
                    return;
                }

                if (string.Equals(filterContext.HttpContext.Request.Headers["X-Forwarded-Proto"], "https", StringComparison.InvariantCultureIgnoreCase))
                {
                    return;
                }

                if (filterContext.HttpContext.Request.IsLocal)
                {
                    return;
                }

                var val = ConfigurationManager.AppSettings["RequireSSL"].Trim();
                var requireSsl = bool.Parse(val);
                if (!requireSsl)
                {
                    return;
                }
            }

            base.OnAuthorization(filterContext);
        }
    }

我还更新了ExitHttps属性。有类似的问题..。

代码语言:javascript
运行
复制
public class ExitHttpsAttribute : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentException("Filter Context");
            }

            if (filterContext.HttpContext == null)
            {
                return;
            }

            var isSecure = filterContext.HttpContext.Request.IsSecureConnection;

            var currentUrl = filterContext.HttpContext.Request.Url;
            if (!isSecure && currentUrl.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.CurrentCultureIgnoreCase))
            {
                isSecure = true;
            }

            if (!isSecure && string.Equals(filterContext.HttpContext.Request.Headers["X-Forwarded-Proto"], "https", StringComparison.InvariantCultureIgnoreCase))
            {
                isSecure = true;
            }

            if (isSecure)
            {
                //in these cases keep https
                // abort if a [RequireHttps] attribute is applied to controller or action
                if (filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof (RequireHttpsAttribute), true).Length > 0)
                {
                    isSecure = false;
                }

                if (isSecure && filterContext.ActionDescriptor.GetCustomAttributes(typeof (RequireHttpsAttribute), true).Length > 0)
                {
                    isSecure = false;
                }

                // abort if a [RetainHttps] attribute is applied to controller or action
                if (isSecure && filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof (RetainHttpsAttribute), true).Length > 0)
                {
                    isSecure = false;
                }

                if (isSecure && filterContext.ActionDescriptor.GetCustomAttributes(typeof (RetainHttpsAttribute), true).Length > 0)
                {
                    isSecure = false;
                }

                // abort if it's not a GET request - we don't want to be redirecting on a form post
                if (isSecure && !String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
                {
                    isSecure = false;
                }
            }

            if (!isSecure)
            {
                return;
            }

            // redirect to HTTP
            var url = "http://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
            filterContext.Result = new RedirectResult(url);
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26849776

复制
相关文章

相似问题

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