Membership Get Users Email Membership provides way to store and validate user and manage authentication in web application. Many times we need to retrieve user’s email address using by user name. Here is one Csharp utility function example to retrieve users email.

Example to get user’s email

public static string GetUserEmail(string userName)
    int count;
    var userCollection = System.Web.Security.Membership.Provider.GetAllUsers(0, 999, out count);
    var users = userCollection.Cast<System.Web.Security.MembershipUser>().ToList();
    var user = users.FirstOrDefault(u => u.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase));

    if(user != null)
        return user.Email;
    return userName;

In above function if user is not found then it will return user name.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.