10

我正在尝试在 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);
4

2 回答 2

17

尝试更新用户:

var user = Membership.GetUser(id);
user.Email = "new@email.com";
Membership.UpdateUser(user);
于 2012-04-20T11:12:23.390 回答
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)
于 2020-06-08T07:49:59.767 回答