如何使用ASP.Net会员制供应商添加一个额外的字段?

 2023-02-16    391  

问题描述

我有一个基本的应用程序,使用ASP.NET隶属提供程序.默认情况下,您可以使用少数字段,例如用户名,密码,记住我.

如何添加到 asp.net隶属提供程序所以我可以在寄存器部分中添加另一个字段”地址”,并将其保存到数据库?

如何使用ASP.Net会员制供应商添加一个额外的字段?

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

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

在创建用户函数中,我也想保存用户的”地址”.

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

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

任何想法?

推荐答案

作为jwsample,您可以使用配置文件提供程序.

或者,您可以创建存储其他与用户相关信息的您自己的表.这是一点工作,因为您在挂钩中创建自己的表并创建代码以获取和从这些表中保存数据,但我发现以这种方式使用自定义表允许更大的灵活性和更高的可维护性而不是配置文件提供商(具体地,默认配置文件提供程序,SQLProfileProvider,它以低效,非正式化方式存储配置文件数据).

看看本教程,我走过这个过程:存储附加用户信息.

其他推荐答案

您将要使用配置文件提供程序,即它的意义.如果修改会员提供程序以添加其他字段,它将打破提供程序合同,并且将来将无法切换到另一个.

以下是配置文件提供程序: http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx
更多信息: http://msdn.microsoft.com/en-us/library/014bec1k.aspx

如果您使用的是SQL成员资格提供程序,可能只安装了所有表格结构以支持个人资料提供商.

其他推荐答案

添加名为”地址”的新列:

步骤1: models/identitymodels.cs

将以下代码添加到”ApplicationUser”类:

public string Address{ get; set; }

步骤2: models/consureviewmodels.cs

将以下代码添加到”注册验证模型”类:

public string Address{ get; set; }

步骤3:视图/register.chtml

将地址输入文本框添加到视图:

<div class="form-group">
        @Html.LabelFor(m => m.Address, new { @class= "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Address, new { @class= "form-control" })
        </div>
</div>

步骤4:

go to tools> nuget manager> package manager控制台

步骤答:键入”enable-迁移”,然后按Enter
步骤b:键入”添加迁移”地址””,然后按Enter键
步骤c:键入”更新 – 数据库”,然后按Enter

i.e

PM> Enable-Migrations
PM> Add-Migration "Address"
PM> Update-Database

步骤5:控制器/accountcontroller.cs

转到注册操作并将”地址= model.address”添加到ApplicationUser
I.E

var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Address= model.Address}

以上所述是小编给大家介绍的如何使用ASP.Net会员制供应商添加一个额外的字段?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/33935.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。