10

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);
1
  • I figured it out: // change email MembershipUser u = Membership.GetUser(User.Identity.Name); u.Email = email; System.Web.Security.Membership.UpdateUser(u); -- I have too little reputation to answer my own question within 8 hours though.
    – Ecnalyr
    Apr 20, 2012 at 11:10

2 Answers 2

17

Try UpdateUser:

var user = Membership.GetUser(id);
user.Email = "new@email.com";
Membership.UpdateUser(user);
2
  • Thank you. System.Web.Security.Membership.UpdateUser(u);
    – Ecnalyr
    Apr 20, 2012 at 11:24
  • I too am having the same problem as @Lakshay. This does NOT update my user's membership information.
    – Eric K
    Jan 19, 2015 at 15:07
0

I'd like to emphasise a point, following on from @Ecnalyr and @jwaern. When changing the email address, assigning the email address directly as follows did not work:

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

First, as shown in the responses above, one must assign the MembershipUser to a variable, and only then affect an UpdateUser on that variable.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.