简体   繁体   English

自定义ASP.Net成员资格和登录控件

[英]Custom ASP.Net Membership and the Login control

I am creating a custom membership provider for a web app that already has it's users stored in an existing database table. 我正在为已经有用户存储在现有数据库表中的Web应用程序创建自定义成员资格提供程序。 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. 我的自定义提供程序具有ValidateUser()的重写方法,此刻我仅在此处返回true。 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. 我猜另一个选择是使用ASP.Net配置文件提供程序,但同样我不清楚在哪里挂入登录进程来运行一些代码,这些代码要么创建此用户对象,要么为当前用户填充配置文件信息。

使用“关注点分离”的原理,您的成员资格提供者不应在Session中存储任何用户信息。

As John said, don't make your provider code store the user information in Session. 如John所说,不要让您的提供者代码在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. 相反,您可以使用Login控件( 此处有更多详细信息),如果一切配置正确,登录成功,它将使用您的提供程序(在您的情况下,这是因为您返回true ),您可以通过调用提供程序的GetUser方法在OnLoggedIn事件处理程序中获取用户,并存储在Session中返回的MembershipUser。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM