2008-10-26 6 views
10

MVC 계정 컨트롤러를보고 있는데 ASP.NET 웹 양식에서 나온 것 같습니다. 그것을 사용하는 방법에 대한 좋은 배경 정보가 있습니까?ASP.NET MVC 계정 컨트롤러 사용 지침?

사용자 데이터베이스 테이블에 매핑 할 수 있습니까? 아니면 자신의 사용자 관리를 게시하는 것이 더 좋습니까?

어떻게 MVC에서 로그인 된 사용자가 볼 수있는 페이지를 제한 할 수 있습니까? 그걸 모두 굴려야합니까?

ASP.NET 회원 자격을 이해하는 데 웹에서 제공하는 리소스는 무엇입니까?

답변

18

내가 MVC 계정 컨트롤러 찾고 있어요 .... asp.net에서 것 같다?

Scott Guthrie는 그의 블로그 항목 ASP.NET MVC Preview 4에 대해 이렇게 설명합니다. 그는 기본적으로 MVC 샘플의 계정 컨트롤러가 ASP.NET 멤버 자격 공급자를 사용하므로 어떤 멤버도 사용할 수 있다고 말합니다. (인터넷에서 ASP.NET 멤버쉽 공급자에 대해 자세히 알 수 있다고 생각합니다.) 그 중 하나를 구현하거나 사용하고 싶지 않으면 응용 프로그램을 수정하여 자신의 사용자 관리를 사용하는 것이 가장 좋은 방법 일 것입니다.

어떻게 사용자 로그인 볼 수있는 페이지 제한이 에 MVC에서의 사용을해야합니까? 너 자신에 모든 것을 굴려야합니까?

컨트롤러 클래스 또는 동작 방법에 Authorize 특성을 추가 할 수 있습니다. (위와 동일 source)

// Only logged in users can access this controller. 
[Authorize] 
public class SomeController : Controller 
{ 
    #region Not really important for this example. :] 
    // Maybe rather use a BLL service here instead of the repository from the DAL, but this example is already more verbose than required. 
    private IStuffRepository stuffRepository; 

    public SomeController(IStuffRepository stuffRepository) 
    { 
     if (null == stuffRepository) 
     { 
      throw new ArgumentNullException("stuffRepository"); 
     } 

     this.stuffRepository = stuffRepository; 
    } 
    #endregion 

    // The authorize attribute is inherited - only logged in users can use the index action. 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    // Moderators can flag stuff. 
    [Authorize(Roles="Moderator")] 
    public ActionResult Flag(int id) 
    { 
     this.stuffRepository.Flag(id); 
     return RedirectToAction("Index"); 
    } 

    // Admins ans SysOps can delete stuff. 
    [Authorize(Roles="Admin,SysOp")] 
    public ActionResult Delete(int id) 
    { 
     this.stuffRepository.Delete(id); 
     return RedirectToAction("Index"); 
    } 

    // Only joed can change the objects stuff. ;) 
    // (This is probably bullshit, of course, but I could not make any better example. I blame the fact it is late at night. :)) 
    [Authorize(Users="COMPANY\\joed")] 
    public ActionResult ChangeId(int oldId, int newId) 
    { 
     this.stuffRepository.ChangeId(oldId, newId); 
     return RedirectToAction("Index"); 
    } 
} 
+0

멋진 예, DI 및 훌륭한 의견! +1 – Matt