自定义 ASP.Net 成员身份和登录控件

发布于 2024-08-25 05:42:53 字数 294 浏览 4 评论 0原文

我正在为一个 Web 应用程序创建一个自定义会员资格提供程序,该应用程序已将其用户存储在现有数据库表中。我使用了教程中的一些代码来帮助启动我的提供程序,但我对如何与实际登录过程进行交互有点迷失。

我的自定义提供程序有一个 ValidateUser() 的重写方法,目前我只是在那里返回 true 。但我想创建一个当前用户对象来存储在会话范围中。该对象将仅存储有关用户的一些详细信息。

我想另一个选择是使用 ASP.Net 配置文件提供程序,但我再次不清楚在哪里挂钩登录进程来运行一些代码,这些代码将创建此用户对象或填充当前用户的配置文件信息。

I am creating a custom membership provider for a web app that already has it's users stored in an existing database table. I used some code from a tutorial to help jump start my provider but I am a bit lost on how i can interact with the actual log in process.

My custom provider has an override method for ValidateUser() and at the moment I am just returning true there. But I want to create a current user object to store in session scope. This object will just store some specifics about the user.

I guess another option would be to use the ASP.Net profile provider but again I am not clear on where to hook into log in process to run some code that would either create this user object or populate the profile information for the current user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦过后 2024-09-01 05:42:53

正如约翰所说,不要让您的提供者代码将用户信息存储在会话中。相反,您可以使用登录控件(此处 你有更多关于它的细节),如果一切配置正确,它将使用你的提供程序,并且如果登录成功(在你的情况下,这将是因为你返回 true)你可以获取用户在 OnLoggedIn 事件处理程序中,通过调用提供程序的 GetUser 方法,并将返回的 MembershipUser 存储在 Session 中。

您的代码可能类似于以下内容:

protected void LoginCtrl_LoggedIn(object sender, EventArgs e)
{
    var user = Membership.GetUser(LoginCtrl.UserName, true);
    Session["CurrentUser"] = user;
}

As John said, don't make your provider code store the user information in Session. Instead, you can use a Login control (here you have some more details about it), it will be using your provider if everything is correctly configured, and if the login is successful (in your case it will be because you're returning true) you can get the user in the OnLoggedIn event handler, by calling the GetUser method of the provider, and store the MembershipUser returned in Session.

Your code could look something similar to this:

protected void LoginCtrl_LoggedIn(object sender, EventArgs e)
{
    var user = Membership.GetUser(LoginCtrl.UserName, true);
    Session["CurrentUser"] = user;
}
晚雾 2024-09-01 05:42:53

使用“关注点分离”的原则,您的会员提供商不应在会话中存储任何用户信息。

Using the principal of "separation of concerns", your membership provider shouldn't be storing any user information in Session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文