本文介绍了MVC3,RequireHttps和自定义处理导致HTTP 310的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图构建一个使用SSL连接的Web应用程序。所以,我做了一些研究,发现我可以使用RequireHttpsAttribute类来实现我所需要的。事情是,当我使用它,应用结果在310错误的执行(太多重定向)。我甚至建立一个自定义的类来处理从http切换到https。但是,这也导致错误。

我的类来处理TE协议开关:

 公共类RequireSSLAttribute
    继承ActionFilterAttribute    公共财产IsRequired()为布尔    公共覆盖子OnActionExecuting(filterContext作为ActionExecutingContext)
        如果Me.IsRequired AndAlso运算filterContext.HttpContext.Request.Url.Scheme<> HTTPS然后
            filterContext.HttpContext.Response.Redirect(filterContext.HttpContext.Request.Url.OriginalString.Replace(\"http:\", https:开头)。删除(filterContext.HttpContext.Request.Url.OriginalString.LastIndexOf(:)+ 1),真)
            filterContext.Result =新HttpUnauthorizedResult
        万一
    结束小组    的Public Sub New()
        IsRequired = TRUE
    结束小组
末级


解决方案

我不知道你的主机是谁,但我只是碰到了对的并在他们的发现了这个:

They have also provided an example solution on Github here, which I will copy below for convenience:

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);
        }
    }
}

I'm not sure if this will solve your problem; but perhaps even if you aren't using AppHarbor the root cause may be the same for you, in which case the above seems worth a shot.

这篇关于MVC3,RequireHttps和自定义处理导致HTTP 310的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:22