【问题标题】:My web application is redirecting to secure and I can't stop it我的 Web 应用程序正在重定向到安全,我无法阻止它
【发布时间】:2013-06-27 20:16:34
【问题描述】:

我有一个 MVC4 应用程序。在我的基本控制器上,我有一个条件重定向到安全 WebPageRequireHttpsAttribute,而不是在 DEBUG 中,所有其他页面服务控制器都继承如下:

#if !DEBUG
    [WebPageRequireHttps]
#endif
    public abstract class SecureController : Controller
    {
        ...

我的WebPageRequireHttpsAttribute 是这样定义的:

[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes",
    Justification = "Unsealed because type contains virtual extensibility points.")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class WebPageRequireHttpsAttribute : FilterAttribute, IAuthorizationFilter
{
    public virtual void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }

        if (!filterContext.HttpContext.Request.IsSecureConnection)
        {
            HandleNonHttpsRequest(filterContext);
        }
    }

    protected virtual void HandleNonHttpsRequest(AuthorizationContext filterContext)
    {
        // only redirect for GET requests, otherwise the browser might not propagate the verb and request
        // body correctly.

        if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException(
                "Only redirect for GET requests, otherwise the browser might not propagate the verb and request body correctly.");
        }

        // redirect to HTTPS version of page
        if (filterContext.HttpContext.Request.Url == null) return;
        var url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
        filterContext.Result = new RedirectResult(url, true);
    }

就是这样。这是对网页进行重定向的唯一点(我对 webapi 页面有类似的东西)。

让我的站点处于 DEBUG 模式,并设置了 DEBUG 常量,我被重定向到 HTTPS 页面,这在我的开发盒中当然不存在,我完全不知道为什么。我已经注释掉了这个属性,我什至删除了这个类,它仍然被重定向。我在这里拉头发。

IIS Express 可以缓存一些奇怪的东西吗?是否可以将重定向属性应用为所有请求的过滤器,而不管它是否被调用?这让我发疯了。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 https iis-express


    【解决方案1】:

    好的,答案与 Visual Studio、C# 或 ASP.NET MVC 无关……而是我的默认浏览器,即 Chrome。我发送了一个 301(永久重定向),Chrome 缓存了它。在正常情况下,这将是一件好事;对于开发工作,没那么多。

    要从 Chrome 中删除缓存的重定向,我打开了 Chrome 选项,设置,单击“显示高级设置”。在隐私下,我点击了“清除浏览数据...”按钮,并检查了以下选项:

    • 清除浏览历史记录
    • 清除下载历史记录
    • 删除 cookie 和其他网站和插件数据
    • 清空缓存

    并将下拉时间段设置为 1 周。然后我再次点击“清除浏览数据”按钮。

    我很确定我只需要其中一个选项,但我对使用霰弹枪方法的这个恼人问题感到非常沮丧。但是,这行得通。

    【讨论】:

    • 如果您不想清除浏览器,可以更改端口(从项目属性中)
    【解决方案2】:

    Global.asax.cs

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new RequireHttpsAttribute()); // <----
    }
    

    我已经在许多使用这种方法的应用程序中强制使用 HTTPS/SSL。

    就是这样,希望对你有帮助。

    【讨论】:

    • ...这不是我要问的。
    • 与他的要求完全相反。
    猜你喜欢
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2017-07-26
    相关资源
    最近更新 更多