ASP.NET MVC 2 Preview 2 Futures RequireHttps 属性を使用するにはどうすればよいですか?
セキュリティで保護されていない HTTP リクエストがアクション メソッドに送信されないようにしたいと考えています。自動的に HTTPS にリダイレクトしたい。
MSDN:
この機能を使用するにはどうすればよいですか?
ASP.NET MVC 2 Preview 2 Futures RequireHttps 属性を使用するにはどうすればよいですか?
セキュリティで保護されていない HTTP リクエストがアクション メソッドに送信されないようにしたいと考えています。自動的に HTTPS にリダイレクトしたい。
MSDN:
この機能を使用するにはどうすればよいですか?
そのためには、自分でロールバックする必要があると思いますActionFilterAttribute
。
public class RedirectHttps : ActionFilterAttribute {
public override void OnActionExecuting(ActionExecutingContext filterContext) {
if (!filterContext.HttpContext.Request.IsSecureConnection) {
filterContext.Result =
new RedirectResult(filterContext.HttpContext.Request.Url.
ToString().Replace("http:", "https:"));
filterContext.Result.ExecuteResult(filterContext);
}
base.OnActionExecuting(filterContext);
}
}
次に、コントローラーで:
public class HomeController : Controller {
[RedirectHttps]
public ActionResult SecuredAction() {
return View();
}
}
これも読みたくなるかもしれません。
私の推測:
[RequireHttps] //apply to all actions in controller
public class SomeController
{
//... or ...
[RequireHttps] //apply to this action only
public ActionResult SomeAction()
{
}
}