Search code examples
c#asp.netasp.net-mvc-4simplemembershipmembershipuser

MembershipUser.Email returns null even when email is stored in database


my problem is, that I am not able to access the user's email, when using Membership.GetUser(username) method. I need it when I am reset password. I receive the username in POST request. My code looks like this:

...
System.Web.Security.MembershipUser user = System.Web.Security.Membership.GetUser(model.UserName);
    if (user != null)
     {   
         string emailAddress = user.Email;
...

I am sure that the user to whom I am trying to reset the password, has the email stored in database, but when I want to retrieve it user.Email is null.

Can somebody help me with this problem? Thank you in advance.


Solution

  • The methods you are using are for traditional ASP.NET membership, not SimpleMembership. Here is the way to get that information from SimpleMembership.

    var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
    var email = user.Email;
    

    You can read more about accessing and customizing UserProfile here.

    There is also an article on setting up password reset for SimpleMembership here and the source code is available here.