如何使用ASP.Net成员资格提供程序添加一个额外的字段? [英] How do I add an extra field using ASP.Net membership provider?

查看:133
本文介绍了如何使用ASP.Net成员资格提供程序添加一个额外的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ASP.NET成员资格提供程序的基本应用程序.默认情况下,您可以使用一些字段,例如用户名,密码,请记住我.

I have a basic app using the ASP.NET membership provider. By default, you can use a few fields such as username, password, remember me.

如何添加到 asp.net成员资格提供者,以便可以在注册部分中添加其他字段地址"并将其保存到数据库中?

How do I add to the asp.net membership provider so I can add an additional field "address" in the register section and have it save to the database?

在帐户模型中创建用户时,我目前看到以下内容:

I currently see the following when creating a user in the account model:

public MembershipCreateStatus CreateUser(string userName, string password,
   string email)
{
   if (String.IsNullOrEmpty(userName))
   { throw new ArgumentException("Value cannot be null or empty.", "userName"); }
   if (String.IsNullOrEmpty(password))
   { throw new ArgumentException("Value cannot be null or empty.", "password"); }
   if (String.IsNullOrEmpty(email))
   { throw new ArgumentException("Value cannot be null or empty.", "email"); }

   MembershipCreateStatus status;
   _provider.CreateUser(userName, password, email, null, null, true,
       null, out status);

   return status;
}

在创建用户"功能中,我还想保存用户的地址".

In the create user function, I also want to save the user's "address".

register.aspx 中,我添加了以下内容:

In the register.aspx I added a the following:

<div class="editor-label">
    <%: Html.LabelFor(m => m.Address) %>
</div>
<div class="editor-field">
    <%: Html.TextAreaFor(m => m.Address) %>
</div>

有什么想法吗?

推荐答案

正如jwsample所述,您可以为此使用配置文件提供程序.

As jwsample noted, you can use the Profile Provider for this.

或者,您可以创建自己的表来存储其他与用户相关的信息.由于您需要创建自己的表并创建用于在这些表之间获取和保存数据的代码,因此需要做更多的工作,但是我发现以这种方式使用自定义表可以提供更大的灵活性和更多的可维护性比配置文件提供程序(特别是默认配置文件提供程序SqlProfileProvider,它以低效,非规范化的方式存储配置文件数据).

Alternatively, you could create your own table(s) that store additional user-related information. This is a little more work since you're on the hook for creating your own tables and creating the code to get and save data to and from these tables, but I find that using custom tables in this way allows for greater flexibility and more maintainability than the Profile Provider (specifically, the default Profile Provider, SqlProfileProvider, which stores profile data in an inefficient, denormalized manner).

看一下本教程,我将逐步完成该过程:

Take a look at this tutorial, where I walk through this process: Storing Additional User Information.

这篇关于如何使用ASP.Net成员资格提供程序添加一个额外的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆