问题 如何使用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>
有任何想法吗?
4340
2017-08-08 19:45
起源
答案:
正如jwsample所述,您可以使用配置文件提供程序。
或者,您可以创建自己的表来存储其他与用户相关的信息。这是一个更多的工作,因为您已经开始创建自己的表并创建代码以从这些表获取和保存数据,但我发现以这种方式使用自定义表允许更大的灵活性和更高的可维护性而不是Profile Provider(特别是默认的Profile Provider,SqlProfileProvider,它以低效的非规范化方式存储配置文件数据)。
看一下本教程,我将在这个过程中完成这个过程: 存储其他用户信息。
9
2017-08-16 15:52
您将要使用配置文件提供程序,这就是它的意思。如果您修改成员资格提供程序以添加附加字段,则会破坏提供程序合同,并且您将来无法切换到另一个字段。
这是个人资料提供者: http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx
更多信息: http://msdn.microsoft.com/en-us/library/014bec1k.aspx
如果您使用的是sql成员资格提供程序,那么您可能已安装所有表结构以支持配置文件提供程序。
3
2017-08-08 20:08
要添加名为“Address”的新列:
步骤1: 型号/ IdentityModels.cs
将以下代码添加到“ApplicationUser”类:
public string Address{ get; set; }
第2步: 型号/ AccountViewModels.cs
将以下代码添加到“RegisterViewModel”类:
public string Address{ get; set; }
第3步: 查看/ Register.cshtml
将地址输入文本框添加到视图:
<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 :
转到工具> NuGet管理器>程序包管理器控制台
步骤A:键入“Enable-Migrations”并按Enter键
步骤B:输入“添加 - 迁移”地址“”并按Enter键
步骤C:键入“Update-Database”并按Enter键
即
PM> Enable-Migrations
PM> Add-Migration "Address"
PM> Update-Database
第5步: 控制器/ AccountController.cs
转到“注册操作”并将“Address = model.Address”添加到ApplicationUser
即
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Address= model.Address}
2
2018-02-22 08:21
答案:
正如jwsample所述,您可以使用配置文件提供程序。
或者,您可以创建自己的表来存储其他与用户相关的信息。这是一个更多的工作,因为您已经开始创建自己的表并创建代码以从这些表获取和保存数据,但我发现以这种方式使用自定义表允许更大的灵活性和更高的可维护性而不是Profile Provider(特别是默认的Profile Provider,SqlProfileProvider,它以低效的非规范化方式存储配置文件数据)。
看一下本教程,我将在这个过程中完成这个过程: 存储其他用户信息。
9
2017-08-16 15:52
您将要使用配置文件提供程序,这就是它的意思。如果您修改成员资格提供程序以添加附加字段,则会破坏提供程序合同,并且您将来无法切换到另一个字段。
这是个人资料提供者: http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx
更多信息: http://msdn.microsoft.com/en-us/library/014bec1k.aspx
如果您使用的是sql成员资格提供程序,那么您可能已安装所有表结构以支持配置文件提供程序。
3
2017-08-08 20:08
要添加名为“Address”的新列:
步骤1: 型号/ IdentityModels.cs
将以下代码添加到“ApplicationUser”类:
public string Address{ get; set; }
第2步: 型号/ AccountViewModels.cs
将以下代码添加到“RegisterViewModel”类:
public string Address{ get; set; }
第3步: 查看/ Register.cshtml
将地址输入文本框添加到视图:
<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 :
转到工具> NuGet管理器>程序包管理器控制台
步骤A:键入“Enable-Migrations”并按Enter键
步骤B:输入“添加 - 迁移”地址“”并按Enter键
步骤C:键入“Update-Database”并按Enter键
即
PM> Enable-Migrations
PM> Add-Migration "Address"
PM> Update-Database
第5步: 控制器/ AccountController.cs
转到“注册操作”并将“Address = model.Address”添加到ApplicationUser
即
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Address= model.Address}
2
2018-02-22 08:21