ASP.NET:带有自定义用户表的自定义 MembershipProvider
我最近开始修补 ASP.NET MVC,但这个问题也应该适用于经典的 ASP.NET。无论如何,我对表单身份验证和成员资格提供商也不太了解。
我正在尝试编写自己的 MembershipProvider
,它将连接到数据库中我自己的自定义用户表。我的用户表包含所有基本用户信息,例如用户名、密码、密码盐、电子邮件地址等,还包含名字、姓氏和居住国家/地区等信息。
据我了解,在 ASP.NET 中执行此操作的标准方法是创建用户表 没有额外信息,然后是包含额外信息的“配置文件”表。然而,这对我来说听起来不太好,因为每当我需要访问这些额外信息时,我都必须进行一次额外的数据库查询才能获取它。
我在《Pro ASP.NET 3.5 in C# 2008》一书中读到,如果您需要大量访问配置文件表并且网站中有许多不同的页面,那么为配置文件设置一个单独的表并不是一个好主意。
现在解决手头的问题...正如我所说,我正在编写自己的自定义 MembershipProvider
子类,到目前为止进展顺利,但现在我开始意识到 CreateUser
不允许我以我想要的方式创建用户。该方法仅接受固定数量的参数,名字、姓氏和居住国家不属于其中。
那么,如果我的 MembershipProvider
的 CreateUser
中没有此信息,我该如何在自定义表中为新用户创建条目呢?
I've recently started tinkering with ASP.NET MVC, but this question should apply to classic ASP.NET as well. For what it's worth, I don't know very much about forms authentication and membership providers either.
I'm trying to write my own MembershipProvider
which will be connected to my own custom user table in my database. My user table contains all of the basic user information such as usernames, passwords, password salts, e-mail addresses and so on, but also information such as first name, last name and country of residence.
As far as I understand, the standard way of doing this in ASP.NET is to create a user table
without the extra information and then a "profile" table with the extra information. However, this doesn't sound very good to me, because whenever I need to access that extra information I would have to make one extra database query to get it.
I read in the book "Pro ASP.NET 3.5 in C# 2008" that having a separate table for the profiles is not a very good idea if you need to access the profile table a lot and have many different pages in your website.
Now for the problem at hand... As I said, I'm writing my own custom MembershipProvider
subclass and it's going pretty well so far, but now I've come to realize that the CreateUser
doesn't allow me to create users in the way I'd like. The method only takes a fixed number of arguments and first name, last name and country of residence are not part of them.
So how would I create an entry for the new user in my custom table without this information at hand in CreateUser
of my MembershipProvider
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该继续采用您的方法并在实现中添加一个新函数,我的意思是,重载 CreateUser 方法并使用 CustomMembershipUser (扩展 MembershipUser )作为参数。
这样,在使用提供程序之前,将其转换为您的 CustomMembershipProvider 并使用重载方法。
I think you should go on with your approach and add a new function in your implementation, I mean, overload the CreateUser method and have a CustomMembershipUser (that extends the MembershipUser) as a parameter.
In that way, before using the provider, cast it to your CustomMembershipProvider and use the overloaded method.
我同意您的分析,您应该将会员资格和个人资料信息保留在同一个表中。由于您受到 CreateUser 所采用的参数数量的限制是正确的,因此您需要设计字段,以便非成员资格配置文件属性可为空。但是,这并不意味着数据库中的必填字段将为空。相反,您可以使用以下代码片段:
通过这种方式,您可以利用 ASP.NET 的成员资格提供程序来创建用户并保存配置文件数据,但对于最终用户来说,这是一个原子操作。
I agree with your analysis that you should keep both membership and profile information in the same table. Since you are correct that you are restricted by the number of parameters that CreateUser takes, you will need to design your field so that non-membership profile attributes are nullable. This does not mean that you will have required fields that are null in the database, however. Instead, you can you the below snippet:
In this way, you leverage ASP.NET's membership providers to create the user and save profile data, but to your end user it is a single atomic operation.