Saving an email change within the default Membership Provider in ASP.NET MVC

Ecnalyr picture Ecnalyr · Apr 20, 2012 · Viewed 7.4k times · Source

I am trying to set and save an email change using within the Membership Provider in ASP.NET MVC 3. I do not know how to properly set and change the email property within the Membership Provider.

MSDN seems to suggest the MembershipUser.Email Property as it, "Gets or sets the e-mail address for the membership user," but I do not know how to get this to function properly.

I am attempting to use this code:

// 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. . .

EDIT:

I figured it out:

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

Answer

jwaern picture jwaern · Apr 20, 2012

Try UpdateUser:

var user = Membership.GetUser(id);
user.Email = "[email protected]";
Membership.UpdateUser(user);