양식 인증 - 객체 데이터 소스를 사용하여 FormView의 사용자 정보에 현재 로그인 된 표시
-
06-07-2019 - |
문제
내 웹 응용 프로그램에서 쿠키와 함께 양식 인증을 사용합니다. 페이지에서 현재 로그인 한 사용자의 정보를 ObjectDatasource로 구동하는 Formview 내부에 표시하고 싶습니다. 내 데이터 소스에는 데이터베이스에서 사용자 데이터를 요청할 사용자 이름을 매개 변수로 수락하는 선택 메소드가 있습니다. 현재 로그인 한 사용자의 사용자 이름을 얻고 데이터 소스의 선택 매개 변수로 사용하는 방법
해결책 2
대신, 데이터 소스 선택 이벤트를 사용하고 INPUTPARAMETER로 필요한 정보를 추가하기로 선택했습니다.
다른 팁
global.asax에서 .. 당신은 다음을 작성해야합니다.
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를 사용하면 데이터 액세스 계층 클래스를 작성 하고이 클래스의 일부 방법을 데이터 소스 작업에 매핑 할 수 있습니다. Theses 메소드 내에서 httpcontext.current.user에 액세스 할 수 있습니다.
당신이 말했듯이 당신이 "내 웹 응용 프로그램에서 나는 쿠키와 함께 양식 인증을 사용합니다"라고 말했습니다. 사용자를 "로그인"하고 쿠키를 브라우저로 보내는 방법을 알고 있다고 가정합니다. 문제가 있으시면 알려주세요.
제휴하지 않습니다 StackOverflow