我正在尝试构建一个使用 SSL 连接的 Web 应用程序。所以我做了一些研究,发现我可以使用 RequireHttpsAttribute 类来实现我所需要的。问题是,当我使用它时,应用程序的执行会导致 310 错误(重定向太多)。我什至构建了一个自定义类来处理从 http 到 https 的切换。但这也会导致错误。
我的类处理协议切换:
Public Class RequireSSLAttribute
Inherits ActionFilterAttribute
Public Property IsRequired() As Boolean
Public Overrides Sub OnActionExecuting(filterContext As ActionExecutingContext)
If Me.IsRequired AndAlso filterContext.HttpContext.Request.Url.Scheme <> "https" Then
filterContext.HttpContext.Response.Redirect(filterContext.HttpContext.Request.Url.OriginalString.Replace("http:", "https:").Remove(filterContext.HttpContext.Request.Url.OriginalString.LastIndexOf(":") + 1), True)
filterContext.Result = New HttpUnauthorizedResult
End If
End Sub
Public Sub New()
IsRequired = True
End Sub
End Class
我不知道你的主人是谁,但我刚刚遇到了类似的问题应用港 https://appharbor.com/并在他们的知识库 http://support.appharbor.com/kb/tips-and-tricks/ssl-and-certificates:
如果您使用内置的 RequireHttpsAttribute 来确保
控制器操作始终使用 HTTPS,您将遇到重定向
环形。原因是 SSL 在负载均衡器级别终止
并且 RequireHttps 无法识别 X-Forwarded-Proto 标头
用于指示请求是使用 HTTPS 发出的。你应该
因此,请使用自定义 RequireHttps 属性来实现此目的。
他们还在 Github 上提供了示例解决方案here https://gist.github.com/915869,为了方便起见,我将其复制如下:
using System;
using System.Web.Mvc;
using RequireHttpsAttributeBase = System.Web.Mvc.RequireHttpsAttribute;
namespace AppHarbor.Web
{
[AttributeUsage(
AttributeTargets.Class | AttributeTargets.Method,
Inherited = true,
AllowMultiple = false)]
public class RequireHttpsAttribute : RequireHttpsAttributeBase
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.HttpContext.Request.IsSecureConnection)
{
return;
}
if (string.Equals(filterContext.HttpContext.Request.Headers["X-Forwarded-Proto"],
"https",
StringComparison.InvariantCultureIgnoreCase))
{
return;
}
if (filterContext.HttpContext.Request.IsLocal)
{
return;
}
HandleNonHttpsRequest(filterContext);
}
}
}
我不确定这是否能解决您的问题;但也许即使您不使用 AppHarbor,根本原因对您来说也可能是相同的,在这种情况下,上述内容似乎值得一试。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)