ASP.NET Membership.GetUserNameByEmail Error: “The E-mail supplied is invalid.”
April 13th, 2012 byWhat the heck does this error message mean?? I had cause to find out when I was getting this error in a method that was simply updating the email address for an existing user. Mind you, I wasn’t creating a new user – I was just updating an existing user. Since the error message is less than helpful, I thought I’d post this so anyone else getting this error won’t have to search as much as I did to find the solution.
Here’s how it came into play: I had created a secure web page to allow my client to manage some of their user accounts. They often had requests to change the email address for the account, so I created a simple form with a couple of text boxes (tbChangeEmail_Old and tbChangeEmail_New), a label to display the status of the attempt (lblChangeEmail_Status) and a Submit button.
Here’s the code I was using, when the user clicked the Submit button:
try
{
string username = Membership.GetUserNameByEmail(tbChangeEmail_Old.Text);
if (!string.IsNullOrEmpty(username))
{
MembershipUser user = Membership.GetUser(username, false);
user.Email = tbChangeEmail_New.Text;
Membership.UpdateUser(user);
lblChangeEmail_Status.Text = "Email address successfully changed.";
}
else
{
lblChangeEmail_Status.Text = "Unable to find user with that email.";
}
}
catch (Exception ex)
{
lblChangeEmail_Status.Text = "Error: " + ex.Message;
}
It’s pretty straightforward – attempt to get the username using the ‘old’ email, and if the account isn’t found, display a message to the user. If we do find the username, then use it to get a MembershipUser object, set the Email property to the ‘new’ value, and update the user. Wrap that up in a try/catch block, and we’re good to go.
Everything worked fine, until today, when my client reported she was getting an error message for one user: “The E-Mail is invalid.” Huh. Did that mean the new email address wasn’t a valid email format? No, the new email was fine. Did it mean the old email didn’t work and the user wasn’t found? No, the test for !string.IsNullOrEmpty(username) catches that, and I verified that the account with the old email address was present. So what’s going on?
I did the usual trick of Googling for a solution, and found a lot of forum questions related to creating a new user, but then I finally found something about DUPLICATE emails, which, of course, are not allowed in the Membership database, based on our configuration. Aha! I did a quick SQL query of the DB, and sure enough, there was already an account with the ‘new’ email address, so it was not possible to update the ‘old’ account with the ‘new’ address!
So, since the existing error message is less than helpful in this case, I changed the logic in the catch block, so I could display a more helpful error on the web page:
catch (Exception ex)
{
if (ex.Message.ToLower() == "the e-mail supplied is invalid.")
lblChangeEmail_Status.Text = "There is another account with the 'new' email address, so the 'old' email account cannot be updated.";
else
lblChangeEmail_Status.Text = "Error: " + ex.Message;
}
Now, if we run across another case like this, my client will know exactly what’s going on. No more ambiguous error messages! J
So, not earth-shattering .NET stuff, but I figure what I learned might help someone else in the same situation. Happy coding!
