2009-10-15 6 views

답변

11

내 생각 엔 :

[RequireHttps] //apply to all actions in controller 
public class SomeController 
{ 
    //... or ... 
    [RequireHttps] //apply to this action only 
    public ActionResult SomeAction() 
    { 
    } 

} 
+1

HTTP 요청을 방지하는 것처럼 보이지만 HTTPS로 리디렉션하지 않습니다. –

+0

아니요. Visual Studio의 ASP.NET 개발 서버에 문제가있을 수 있습니다. http://stackoverflow.com/questions/60113/ –

+5

ASP.NET MVC 요구 사항 : 제작 전용 : http://stackoverflow.com/questions/1639707/asp-net-mvc-requirehttps-in-production-only –

15

나는 당신이 그것에 대해 자신의 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(); 
    } 
} 

당신은뿐만 아니라 this을 읽을 수 있습니다.

+0

POST 메서드를위한 작업에이를 추가 할 때주의하십시오. – Carl

+1

@Carl 왜? 게시물 데이터가 손실 되었기 때문에? 민감한 데이터가 https가 아닌 곳으로 게시되지 않도록하려면 해당 데이터를 처리하지 않아야합니다. – eglasius

+1

@이 스키마를 변경하려면이 방법을 사용하는 것이 좋습니다. 문자열 대체보다 안전해야합니다. http://stackoverflow.com/questions/17968426/changing-the-scheme-of-system-uri –