asp.net-mvcasp.net-mvc-3asp.net-membershipmembership-provider

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


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

Solution

  • Try UpdateUser:

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