开发者

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 m开发者_运维问答y site using email address and he can login using email id and password instead username and password.


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;
        }
    }
}


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. :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜