我有一个 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 可以缓存一些奇怪的东西吗?是否可以将重定向属性应用为所有请求的过滤器,而不管它是否被调用?这真让我抓狂。