Snippet. ASP.NET. How to Find User By Email Using the Membership API

The Membership API is great for working with users. However, when you try to find a user by email it does not come with a straightforward solution. This snippet will find a single user by email.

Method FindUserByEmail

MembershipUser FindUserByEmail(string email)
{
    MembershipUserCollection members = Membership.FindUsersByEmail(email);
    
    if (members.Count > 0)
    { 
        foreach(MembershipUser member in members)
        {
            return member;
        }
    }

    return null;
}

Example

MembershipUser user = FindUserByEmail("someemail@website.org")

if(user==null)
{
    // User with this email does not exist
} 
else
{
    // User with the specified email found.
}

Updated on: 08 Jun 2020