views:

45

answers:

1

The ASP.NET Membershipsystem uses a unique username to identify a user. What if I need to login users by E-Mail?

I could just pass the E-Mail into the property for the username, but then things get a bit messy. The user might want to change the e-mailaddress.

Is there any resource available that decribes a good way to implement it?

+2  A: 

Your approach of using the email address as the username works well for me. But you're right; it does get messy when the email address gets changed. The approach we took was when the user wishes to change their email address, we also change their user name. The only way to do this was through direct database manipulation. We were careful to make sure that any references to the user was done through the UserId; this ensured that the user name change didn't break anything.

Here's the code we used, in case you're interested. The code is in a persistent class that sort of wraps the MembershipUser class and is stored as a one-to-one relationship. Just ignore the evil SQL string building and do something better ;)

IDbCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = string.Format(
    "UPDATE dbo.aspnet_Users SET UserName = '{0}', LoweredUserName = '{0}' WHERE UserId = '{1}'",
    DatabaseManager.SqlEscape(value.ToLower()), this.Id);
cmd.ExecuteNonQuery();

cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = string.Format(
    "UPDATE dbo.aspnet_Membership SET Email = '{0}', LoweredEmail = '{0}' WHERE UserId = '{1}'",
    DatabaseManager.SqlEscape(value.ToLower()), this.Id);
cmd.ExecuteNonQuery();
Jacob