在ASP.NET MVC中的默认成员资格提供程序中保存电子邮件更改

时间:2012-04-20 11:05:42

标签: asp.net-mvc asp.net-mvc-3 asp.net-membership membership-provider

我正在尝试使用ASP.NET MVC 3中的成员资格提供程序设置和保存电子邮件更改。我不知道如何在成员资格提供程序中正确设置和更改电子邮件属性。

MSDN似乎建议使用MembershipUser.Email属性,“获取或设置成员资格用户的电子邮件地址”,但我不知道如何使其正常运行。

我正在尝试使用此代码:

// change email
MembershipUser u = Membership.GetUser(User.Identity.Name);
u.Email = email; // Is this working as expected?
u.Save();  // this line doesn't do anything - what should it be?
db.Save();  <-- that is what I would do with ordinary database changes, but I don't know what context to use for Membership Provider changes. . .

编辑:

我明白了:

// change email
MembershipUser u = Membership.GetUser(User.Identity.Name);
u.Email = email;
System.Web.Security.Membership.UpdateUser(u);

2 个答案:

答案 0 :(得分:16)

尝试使用UpdateUser:

var user = Membership.GetUser(id);
user.Email = "new@email.com";
Membership.UpdateUser(user);

答案 1 :(得分:0)

我想强调一点,紧接着@Ecnalyr和@jwaern。更改电子邮件地址时,按如下所示直接分配电子邮件地址无效:

System.Web.Security.Membership.GetUser.email = newEmail '(this won't work)

首先,如上面的响应所示,必须将MembershipUser分配给变量,然后才影响该变量的UpdateUser。

Dim mu As MembershipUser = System.Web.Security.Membership.GetUser
mu.Email = email
Membership.UpdateUser(mu)