2009-07-15 2 views
3

컨트롤러에서 Authorize 및 RequireSSL (MVC 퓨어에서 제공) 특성을 모두 성공적으로 사용하고 있습니까? 나는 사용자가 로그인하고 실행하기 위해 보안 연결을 사용해야한다는 규칙을 시행해야하는 컨트롤러를 만들었습니다. 사용자가 보안 연결이되어 있지 않으면 앱을 https로 리디렉션해야하므로 RequireSSL 속성에서 Redirect = true를 사용하고 있습니다. (CheckPasswordExpired 내 자체 속성이있다) 같은 코드가 생겼다 :ASP.NET MVC Futures RequireSSL 특성과 함께 특성 승인

[Authorize] 
[RequireSsl(Redirect = true)] 
[CheckPasswordExpired(ActionName = "ChangePassword", 
    ControllerName = "Account")] 
[HandleError] 
public class ActionsController : Controller 
{ 
    .... 
} 

mysite.com/Actions/Index이 사이트에 대한 기본 경로 또한 폼 인증에 대한 리디렉션 할 수있는 기본 페이지입니다.

내가로 이동할 때 사용자가 보안 연결로 리디렉션되고 로그인 페이지로 아직 인증되지 않았기 때문에이를 가져오고 싶습니다. 내가 얻는 것은 HTTP 400 오류 (잘못된 요청)입니다. http://mysite.com/Account/Login으로 이동하면 리디렉션이 작동하지만 내 계정 컨트롤러 또는 로그인 작업 방법에 [인증] 특성이 없습니다.

누구나 내 목표를 달성하기 위해이 두 속성을 함께 사용 해본 경험이 있습니까?

감사합니다.

답변

4

나는 둘 다 성공으로 사용하고 있습니다. 기본 작업에 대한 속성이 있습니까? 답장을

[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)] 
public sealed class RequireSslAttribute : FilterAttribute, IAuthorizationFilter 
{ 
    public RequireSslAttribute() 
    { 
     Redirect = true; 
    } 

    public bool Redirect { get; set; } 

    public void OnAuthorization (AuthorizationContext filterContext) 
    { 
     Validate.IsNotNull (filterContext, "filterContext"); 

     if (!Enable) 
     { 
      return; 
     } 

     if (!filterContext.HttpContext.Request.IsSecureConnection) 
     { 
      // request is not SSL-protected, so throw or redirect 
      if (Redirect) 
      { 
       // form new URL 
       UriBuilder builder = new UriBuilder 
       { 
        Scheme = "https", 
        Host = filterContext.HttpContext.Request.Url.Host, 
        // use the RawUrl since it works with URL Rewriting 
        Path = filterContext.HttpContext.Request.RawUrl 
       }; 
       filterContext.Result = new RedirectResult (builder.ToString()); 
      } 
      else 
      { 
       throw new HttpException ((int)HttpStatusCode.Forbidden, "Access forbidden. The requested resource requires an SSL connection."); 
      } 
     } 
    } 

    public static bool Enable { get; set; } 
} 
+0

감사 : 나는 세계적으로 SSL을 비활성화 할 수 있도록

public class HomeController : BaseController { [Authorize] [RequireSsl] public ActionResult Index() { } } 

은 BTW 나는 선물보다 약간 수정 된 버전을 사용하고 있습니다. 액션 레벨이 아닌 컨트롤러 레벨에 속성이 첨부되어 있습니다. BTW, 당신의 향상 기능이 마음에 듭니다. –

+0

어떻게 사용합니까? 내 dev 환경에서 내가 SSL이되고 싶지 않아요? – Blankman

+0

Validate.IsNotNull (...)은 무엇을합니까? 코드를 붙여 넣었지만 Validate가 없습니다 (나는 future hehe를 설치하고 싶지 않습니다) – Blankman

관련 문제