Get email of user from Asp.net Membership


Example for finding a user email address by user name in asp.net Membership.

/// <summary>
/// Email address by user name
/// </summary>
/// <param name="userName">User name</param>
/// <returns>Email Address</returns>
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;
}