フォーム認証-オブジェクトデータソースを使用して、FormViewに現在ログインしているユーザー情報を表示します
-
06-07-2019 - |
質問
私のWebアプリケーションでは、Cookieでフォーム認証を使用しています。 ObjectDataSourceを使用したFormView内で、現在ログインしているユーザーの情報を表示したいページに。データソースには、データベースからユーザーデータを要求するユーザー名をパラメーターとして受け入れるselectメソッドがあります。現在ログインしているユーザーのユーザー名を取得し、それをデータソースの選択パラメーターとして使用するにはどうすればよいですか。
解決 2
代わりに、データソースのSelectingイベントを使用し、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を使用すると、データアクセスレイヤークラスを記述し、このクラスのいくつかのメソッドをDataSource操作にマッピングできます。これらのメソッド内からHttpContext.Current.Userにアクセスできます。
「ウェブアプリケーションでは、Cookieを使用したフォーム認証を使用しています」とおっしゃいました。 " log"の方法を知っていると思います。ユーザーに送信し、Cookieをブラウザに送信します。問題がある場合はお知らせください。
所属していません StackOverflow