2009-10-25 2 views
0

웹 응용 프로그램에서 쿠키로 양식 인증을 사용합니다. 한 페이지에서 현재 로그인 한 사용자에 대한 정보를 ObjectDataSource에 의해 구동되는 FormView 내에 표시하고자합니다. 내 데이터 소스에는 사용자 데이터를 데이터베이스에서 요청할 사용자 이름을 매개 변수로 받아들이는 select 메소드가 있습니다. 현재 로그인 한 사용자의 사용자 이름을 가져와 데이터 소스의 선택 매개 변수로 사용하려면 어떻게합니까?양식 인증 - 개체 데이터 소스를 사용하여 FormView에 현재 로그인 된 사용자 정보를 표시합니다.

의 Global.asax에

답변

0

대신 데이터 소스의 Selecting 이벤트를 사용하고 필요한 정보를 inputParameter로 추가했습니다.

1

..를 작성해야 :

protected void Application_AuthenticateRequest(object sender, EventArgs e) { 
    if (Request.PhysicalPath.EndsWith(".aspx") || Request.PhysicalPath.EndsWith(".axd")) 
     SecurityManager.SetPrincipal(); 
} 

SecurityManager.SetPrincipal() 메소드처럼 보일 것입니다 : 내가 아는 한

// variable we'll use to set HttpContext.Current.User 
     IPrincipal principal = null; 
     FormsIdentity identity; 

     //IsAuthenticated will be automatically set by .NET framework 
     if (HttpContext.Current.Request.IsAuthenticated) 
     { 
      // (FormsIdentity)HttpContext.Current.User.Identity will 
      // be filled automatically by the .NET framework when using forms authentication 
      identity = (FormsIdentity)HttpContext.Current.User.Identity; 

      // This User class must be defined BY YOU 
      User userProfile; 
      // this user data is the data that you entered when you created the ticket. 
      // this should be a security token that would allow you to GET THE USER FROM IT 
      String userData = (((FormsIdentity)identity).Ticket).UserData; 
      try 
      { 
       // UserHelper is a class that must be able to OBTAIN a USER given a SECURITY TOKEN. 
       // remember, you created this token when you created the ticket you used in the cookie. 
       userProfile = UserHelper.GetUser(userData); 

       // AuthenticatedPrincipal must implement IPrincipal. Consider deriving from GenericPrincipal. 
       // Your IPrincipal implementations must hold a reference to the UserClass you created 
       principal = new AuthenticatedPrincipal(identity, userProfile); 
      } 
      catch 
      { 
       FormsAuthentication.SignOut(); 
       // This is analogous to AuthenticatedPrincipal 
       principal = new AnonymousPrincipal(new GuestIdentity(), UserHelper.GetUser(null)); 
      } 

     } 
     else 
     { 
      principal = new AnonymousPrincipal(new GuestIdentity(), UserHelper.GetUser(null)); 
     } 

     // Now we make our principal, that holds a reference to the currently 
     // logged user, globally visible 
     HttpContext.Current.User = principal; 

, ObjectDataSource를 사용하면 쓸 수 있습니다 Data Access Layer 클래스를 만들고이 클래스의 일부 메서드를 DataSource 작업에 매핑합니다. 이 메서드 내에서 HttpContext.Current.User에 액세스 할 수 있습니다.

당신이 말했듯이 "내 웹 응용 프로그램에서 나는 쿠키로 폼 인증을 사용합니다". 나는 사용자를 "로그"하고 브라우저에 쿠키를 보내는 방법을 알고 있다고 가정합니다. 문제가 있으면 알려주세요.