2012-03-09 2 views
0

저는 MVC에 상당히 익숙하며 아직까지 [Authorize]의 응답을받지 못했습니다. 기본적으로 웹 사이트는 한 영역에서 액세스하기 전에 전자 메일 주소, 이름 및 회사를 제공해야합니다. 비밀번호 없음, 등록 안 함 등등. 그러면 사용자가 이메일 주소와 비밀번호를 사용하여 로그인해야하는 Admin 영역이 생깁니다.여러 승인 요청

내 질문에 이중 인증 상황을 어떻게 구현합니까?

Admin 영역에있을 때 관리자는 사이트가 현재 관리중인 사이트, 즉 현재 사이트가 아닌 다른 사이트로 자료를 업로드 할 수 있습니다. 나는 그들이 관리하는 사이트를 보유하고있는 데이터베이스를 가지고있다. URL은/Admin/Site /와 같지만 일단 관리자로 로그인하면/admin/Site2에 접속할 수 없는지 확인하려면 어떻게해야합니까? Site2은 관리자가 아닙니다.

+0

역할을 사용해야합니다. [http://stackoverflow.com/questions/6404254/mvc-3-dynamic-authorization-of-multiple-roles-and-users][1] [1]에 http : // 유래. com/questions/6404254/mvc-3-multiple-roles-and-users –

답변

1

나는 그 사람이 전에 익명으로있을 수 있고 she/he가 전에 세부 사항을 제공하는 한 파일을 다운로드 할 수 있다고 가정합니다. 이 경우 Authorization 속성을 무시하고 직접 작성하십시오. 다음은 간단한 예입니다. 그것은 설정 될 쿠키에 의존합니다.

public class CheckEmailAddressAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     var request = filterContext.HttpContext.Request; 
     // if the cookie isn't set the a user never provided their details 
     if (request.Cookies["mycookiename"] == null) 
     { 
      // Set it to the correct result, to redirect the user to provide their email address 
      filterContext.Result = new ViewResult { ViewName = "GetEmailAddress" }; 
      filterContext.HttpContext.Response.AppendCookie(new HttpCookie("mycookiename", "true")); 
     } 
     else 
     { 
      // Don't do anything, the user already provided their email address 
     } 

    } 
} 

다운로드 할 컨트롤러에서 이와 같이 지정하십시오.

public class DownloadController : Controller 
{ 
    [CheckEmailAddress] 
    public ActionResult Download(string name) 
    { 
     // Implement download 
    }  
} 

이메일 주소가 설정되면 쿠키를 설정해야합니다. 나는 당신이 그 일을하는 방법을 알고 있다고 생각합니다. "returnUrl"매개 변수가 있는지 확인하여 이메일 주소를 제공 한 사용자를 다운로드 페이지로 리디렉션 할 수도 있습니다.

편집 OP 당으로, 우리가 여기에 업데이트 된 클래스입니다, 사람이 자신의 정보를 입력하면 제외 쿠키를 설정하지 않습니다.

public class CheckEmailAddressAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     var request = filterContext.HttpContext.Request; 
     // if the cookie isn't set the a user never provided their details 
     if (request.Cookies["mycookiename"] == null) 
     { 
      // Set it to the correct result, to redirect the user to provide their email address 
      filterContext.Result = new ViewResult { ViewName = "GetEmailAddress" }; 
      // filterContext.HttpContext.Response.AppendCookie(new HttpCookie("mycookiename", "true")); 
     } 
     else 
     { 
      // Don't do anything, the user already provided their email address 
     } 

    } 
} 
+0

예를 들어, 자세한 내용을 묻는 페이지를 제외한 프레스 영역의 모든 항목에 [CheckEmailAddress]를 넣을 수 있습니까? – ediblecode

+0

또한이 예제를 사용하면 페이지에 가서 쿠키를 설정하기 만하면됩니다. 쿠키는 페이지로 리디렉션 된 다음 뒤로 탐색 할 수 있으며 쿠키가 있습니다. – ediblecode

+0

내 업데이트를 참조하십시오. 속성에 쿠키를 설정하면 안됩니다. – bloudraak