Jer*_*acs 3 c# asp.net-mvc https iis-express asp.net-mvc-4
我有一个MVC4应用程序.在我的基本控制器上,我有一个条件重定向,以便WebPageRequireHttpsAttribute在不在DEBUG中时保证所有其他页面服务控制器继承如下:
#if !DEBUG
[WebPageRequireHttps]
#endif
public abstract class SecureController : Controller
{
...
Run Code Online (Sandbox Code Playgroud)
我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);
}
Run Code Online (Sandbox Code Playgroud)
而已.这是为网页完成重定向的唯一点(我对webapi页面有类似的东西).
我的网站处于DEBUG模式,设置了DEBUG常量,我被重定向到HTTPS页面,当然在我的开发框中不存在,我绝对无法弄清楚原因.我已经注释掉了这个属性,我甚至删除了这个类,它仍然被重定向.我在这里拔头发.
IIS Express可以缓存一些奇怪的东西吗?是否可以将重定向属性应用为所有请求的过滤器,无论是否调用它?这真让我抓狂.
Jer*_*acs 11
好的,答案与Visual Studio,C#或ASP.NET MVC无关......而是我的默认浏览器,即Chrome.我发送了301(永久重定向),Chrome缓存了这个.在正常情况下,这将是一件好事; 对于开发工作,而不是那么多.
要从Chrome中删除缓存重定向,我打开了Chrome选项,设置,点击了"显示高级设置".在隐私下,我点击了"清除浏览数据..."按钮,并选中了以下选项:
并将下拉时间设置为1周.然后我再次点击"清除浏览数据"按钮.
我很确定我只需要其中一个选项,但我对这个烦人的问题感到非常沮丧,因为我使用了霰弹枪的方法.但是,这很有效.
| 归档时间: |
|
| 查看次数: |
1462 次 |
| 最近记录: |