所以我找到了[RequiresHttps]屬性,但是一旦你在https中你的那種卡住了,所以嘗試和能夠對一個URL(和方案)我發現我最終不得不創建自己的擴展控制器來恢復爲不使用[RequireHttps]的動作。在ASP.NET MVC中的HTTP和HTTPS之間移動
只是想知道我在做什麼好或者如果有更好的方法嗎?
public class ExtendedController : Controller
{
protected virtual void HandleHttpRequest(AuthorizationContext filterContext)
{
if (!string.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("Cannot post between https and http.");
}
string url = "http://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
filterContext.Result = new RedirectResult(url);
}
protected override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
object[] attributes = filterContext.ActionDescriptor.GetCustomAttributes(true);
if (!attributes.Any(a => a is RequireHttpsAttribute))
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.HttpContext.Request.IsSecureConnection)
{
this.HandleHttpRequest(filterContext);
}
}
}
}
我剛剛意識到的一件事是我還應該檢查filterContext.IsChildAction - 我想知道是否有可能在子動作上使用RequiresHttp運行相同的問題。似乎需要一個更完整的解決方案,可能在路由一側而不是控制器上。 – Mark 2010-11-25 14:05:11