user685565
user685565

Reputation: 103

Set email address as username in ASP.NET Membership provider

I want to use email address as username in membership api instead of accepting a username. I want that a user can signup to my site using email address and he can login using email id and password instead username and password.

Upvotes: 4

Views: 6958

Answers (3)

lnaie
lnaie

Reputation: 1037

This is all possible as described in this thread already, but there's one thing to be aware of: in the database the username is 50 chars (table Users) long while the email (table Memberships) is 256 chars long. In fact that means shorter email addresses. :)

Upvotes: 5

abatishchev
abatishchev

Reputation: 100288

Just force user name = user email.

To do this just put a RegEx validator on user creation form to allow only proper email as user name.

Rename already existing user names according to users' email.

Upvotes: 4

Chad Ruppert
Chad Ruppert

Reputation: 3680

This is what we did, so its reusable and we can reg it in the web.config. If you think about it, this is where it counts. as long as your validation and frontend indicate to the enduser that their username should be email, and you do proper validation... from there its normal calls to whatever membership provider is backing you. New method for the shortcut version, and hiding/altering the original behind our facade.

public class EmailAsUsernameMembershipProvider : SqlMembershipProvider
{
    public MembershipUser CreateUser(string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        return base.CreateUser(email, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
    }
    private new MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        return base.CreateUser(email, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
    }

    public override bool RequiresUniqueEmail
    {
        get
        {
            return true;
        }
    }
}

Upvotes: 7

Related Questions