使用 ASP.NET MVC 和 SqlMembershipProvider 在哪里存储其他用户详细信息?

发布于 2024-07-27 21:46:18 字数 311 浏览 2 评论 0原文

因此,我正在创建一个 ASP.NET MVC 网站。

它有一个相当复杂的用户注册页面,有很多字段。 我的问题是,我应该在哪里坚持这个? 由会员提供者工具创建的用户表不包含这些列,我对存储有关每个用户的附加信息的最佳实践是什么感到困惑?

这是配置文件提供商发挥作用的地方吗? 它们如何工作? 如果没有,您还如何保留和存储 MembershipProvider 表的 stock 列中未提供的其他用户详细信息?

任何人都可以向我指出一些关于如何处理此问题的资源,如何在需要时访问这些额外的用户详细信息,如何使用所有这些信息创建新用户等。

So, I'm creating an ASP.NET MVC website.

It has a fairly complex user sign-up page with a lot of fields. My question is, where should I persist this? The user tables created by the membership-provider tool don't contain these columns and I'm left confused about what the best practice is in terms of storing this additional information about each user?

Is this where a profile provider comes into play? How do they work? If not, how else do you persist and store the additional user details not provided in the stock columns of the MembershipProvider tables?

Can anyone point me to some resources on how this is handled, how to access those additional user details when I need them, how to create a new user with all this information etc.

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

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

发布评论

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

评论(4

舟遥客 2024-08-03 21:46:18

这就是 ASP.NET 配置文件提供程序的用武之地。您可以使用它来存储您想要的有关用户的任何配置文件信息。 您需要做的就是在 web.config 文件中添加所需的字段, 有关如何在 web.config 文件中配置配置文件字段的 MSDN 链接。 总而言之,您只需将要存储的名称和类型值添加到配置文件元素的属性节点即可。 下面是一个示例:

<profile enabled="true">
  <properties>
    <add name="Name" />
    <group name="Address">
      <add name="Street" />
      <add name="City" />
      <add name="Zip" type="System.Int32" />
    </group>
  </properties>
</profile>

在 ASP.NET Webforms 中,Visual Studio 自动创建一个强类型配置文件类,该类将引用您的自定义配置文件属性。 在 MVC 中,这种情况不会发生。 要引用用户的个人资料信息,只需调用 HttpContext.Profile["PropertyName"]。 示例:

HttpContext.Profile["Name"] = name;
HttpContext.Profile.GetProfileGroup("Address")["Zip"] = zip;

编辑: 正如 Andy 指出的,如果您想对这些属性运行查询,那么使用默认的 SqlProfileProvider 并不是很好。 他是完全正确的,也许我最初应该注意到这个限制。 存在此限制是因为 SqlProfileProvider 将所有配置文件数据存储在三列中:PropertyNames 和 PropertyValuesString/PropertyValuesBinary。 所有键都存储在 PropertyNames 字段中,可以存储为字符串的值存储在 PropertyValuesString 字段中,等等。这意味着执行“Select * from aspnet_Profile where Age > 10”这样的查询非常困难。

This is where the ASP.NET Profile provider comes in. You can use it to store whatever profile information you want about a user. All you need to do is add the fields you need in the web.config file, MSDN Link on how to configure the profile fields in web.config file. To sum the article up, you just add the name and type values you want to store to the properties node of the profile element. Here's an example:

<profile enabled="true">
  <properties>
    <add name="Name" />
    <group name="Address">
      <add name="Street" />
      <add name="City" />
      <add name="Zip" type="System.Int32" />
    </group>
  </properties>
</profile>

In ASP.NET Webforms, Visual Studio automagically creates a strongly typed profile class that will reference your custom profile properties. In MVC, this doesn't happen. To refer to a user's profile information, just call HttpContext.Profile["PropertyName"]. An example:

HttpContext.Profile["Name"] = name;
HttpContext.Profile.GetProfileGroup("Address")["Zip"] = zip;

Edit: As Andy noted, using the default SqlProfileProvider isn't really good if you want to run queries on these properties. He is completely right, and perhaps I should have originally noted this limitation. This limitation exists because SqlProfileProvider stores all of the profile data is in three columns: PropertyNames and PropertyValuesString/PropertyValuesBinary. All of the keys are stored in the PropertyNames field, values that can be stored as a string are stored in the PropertyValuesString field, etc. This means it's extremely hard to do a query like "Select * from aspnet_Profile where Age > 10".

妄断弥空 2024-08-03 21:46:18

我不太确定是否存在最佳实践,这实际上取决于您想要如何使用这些信息。

首先,您必须认识到会员资格和个人资料是两个独立的事物。 ASP.NET 成员资格、配置文件和角色功能被设计为用作服务,为多个站点/应用程序提供服务。

如果您查看这些关系的架构,您会注意到用户对于系统来说是唯一的,但用户可以在应用程序之间共享。 这意味着他们的个人资料信息也会在应用程序之间共享。 成员资格实际上是用户与应用程序的关联,并包含有关他们与该特定应用程序的关系的信息(密码、密码问答等)。

您可以按照 Ryan 建议使用配置文件提供程序,但是 1) 如果您想要收集配置文件指标,则不容易查询该信息;2) 它在会员资格/配置文件服务的所有消费者之间共享。 但是,您可以扩展它以满足您的需求。

您可以按照 Gortok 的建议扩展成员资格提供程序,并且该信息将与应用程序相关,但您需要确保不会通过以更改其接口或方式修改现有存储过程或表来破坏服务的现有使用者。意图。

另一种选择是您可以将其视为一项服务并自行跟踪此信息,使用来自 asp.net sql 提供程序的用户 ID 引用您自己的配置文件实现。

会员资格、个人资料和角色位于 4 Guys from Rolla,我建议您阅读该文章,然后,一旦您熟悉了所有活动部件,就可以就最佳存储位置做出明智的决定以及如何最好地构建您想要创建的个人资料信息。

I'm not really sure there is a best practice and it really depends on how you want to use the information.

First, you have to recognize the membership and profiles are two separate things. The ASP.NET Membership, Profile, and Role functionality is designed to be used as a service, serving multiple sites/applications.

If you look at the schema for these relationships, you'll notice that users are unique to the system but a user can be shared across applications. That means that their profile information is shared across applications as well. Membership is actually the association of a user to an application and contains information about their relationship to that specific application (password, password Q&A, etc).

You can use the profile provider as Ryan suggested, but 1) that information isn't easily queried should you want to gather profile metrics and 2) it is shared across all consumers of the membership/profile services. You can, however, extend it to meet your needs.

You can extend the membership provider as Gortok suggested and that information would be relative to the application but you need to make sure you don't break existing consumers of the service by modifying the existing stored procedures or tables in a fashion that changes their interface or intent.

The other option is you can treat it as a service and track this information on your own, referencing into your own profile implementation using the user id from the asp.net sql provider.

There is a good series (16 parts) on Membership, Profiles, and Roles over at 4 Guys from Rolla that I'd recommend reading and then, once you're familiar with all of the moving parts, make an educated decision on where best to store and how best to structure the profile information you're looking to create.

帥小哥 2024-08-03 21:46:18

我知道这篇文章已经发布很长时间了,问题上下文可能是针对 mvc2 或更早版本的。

现在是 mvc3,我想也许其他寻求 mvc5 上下文答案的人可能会对这个解决方案感兴趣。

在启用了个人用户帐户的标准 mvc5 项目中
您会发现以下代码块可以在 {Project}/Models/IdentityModel.cs 中使用,

// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
}

只需向此类添加更多属性,这些属性将被保存到数据库中
例如。

public class ApplicationUser : IdentityUser
{
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime DateRegistered { get; set; }
}

I know that this post has been posted for a long time, and the question context was probably for mvc2 or earlier.

It is now mvc3, and i thought maybe others seeking answers for mvc5 context might be interested in this solution.

In a standard mvc5 project that has individual user account enabled
You will find the following code block ready for use in {Project}/Models/IdentityModel.cs

// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
}

Simply add in more properties to this class and the properties will be persisted into the database
Eg.

public class ApplicationUser : IdentityUser
{
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime DateRegistered { get; set; }
}
酒废 2024-08-03 21:46:18

文章来自CODE Magazine 帮助我解决了这个问题。

This article from CODE Magazine helped me with this issue.

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