gpt4 book ai didi

c# - 我的 Web 应用程序正在重定向到安全,我无法阻止它

转载 作者:行者123 更新时间:2023-11-30 13:38:21 25 4
gpt4 key购买 nike

我有一个 MVC4 应用程序。在我的基本 Controller 上,我有一个条件重定向到安全 WebPageRequireHttpsAttribute 当不在所有其他页面服务 Controller 继承的 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 可以缓存一些奇怪的东西吗?是否可以将重定向属性用作所有请求的过滤器,而不管它是否被调用?这让我发疯。

最佳答案

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

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

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

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

我很确定我只需要这些选项之一,但我对这个恼人的问题感到非常沮丧,所以我使用了散弹枪方法。但是,这有效。

关于c# - 我的 Web 应用程序正在重定向到安全,我无法阻止它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17392115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com