Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 362
user685565 Avatar asked Dec 04 '22 22:12

user685565


1 Answers

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;
        }
    }
}
like image 152
Chad Ruppert Avatar answered Dec 09 '22 14:12

Chad Ruppert