ASP.NET MEMBERSHIP LOGIN QUESTION - authenticate with either username OR email address

I have an asp.net login control that authenticates a user as follows

TextBox txtUser = (TextBox)Login.FindControl("UsernameEmail");
        string id_user = txtUser.Text;

        TextBox txtPassword = (TextBox)Login.FindControl("Password");
        string pass_user = txtPassword.Text;

if (Membership.ValidateUser(id_user, pass_user))
        {


However I need the user to be able to login with EITHER a username OR an email address.

So in pseudo code I need something like


if txtUser is a valid email address then try and authenticate using email, or if txtUser is a username then try and authenticate using normal username
mugseyAsked:
Who is Participating?
 
S31B1Commented:
The solution might be a very simple one, there is a function which returns the UserName for a given email. If one doesn't exist it returns null!

string usernameFromEmail = Membership.GetUserNameByEmail(id_user);

  if (usernameFromEmail != null)
  {
     Membership.ValidateUser(usernameFromEmail ,pass_user);
  }
  else
  {
     Membership.ValidateUser(id_user,pass_user);
  }
0
 
Jens FiedererTest Developer/ValidatorCommented:
Is your question how to DISTINGUISH the two?

Would the presence of "@" be sufficient?

(Note that the answer depends on what you find acceptable as username and what you find acceptable as email .... in the extreme case where a valid email address IS an acceptable username on your system, the only way to do this would be to possibly do BOTH validations - and fail only if BOTH fail).
0
 
S31B1Commented:
Email validation is a common task in an ASP.NET page where users need to enter their email addresses. Most of the time a@b.c is an accepted email address, but you might like to do better than that? You could use a Regular Expression to test for address the code below gives you gives a lenient Regex pattern for parsing an email address:

string patternLenient = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
mugseyAuthor Commented:
Thanks

So in psuedo would it be like

  if regex is email then
          Membership.ValidateUser(id_email, pass_user))
  else
          Membership.ValidateUser(id_username, pass_user))
//////////////////////



extBox txtUser = (TextBox)Login.FindControl("UsernameEmail");
        string id_user = txtUser.Text;

        TextBox txtPassword = (TextBox)Login.FindControl("Password");
        string pass_user = txtPassword.Text;

if (Membership.ValidateUser(id_user, pass_user))
        {

.................................

How can I test
0
 
S31B1Commented:
How can you test your code?

Or

How can you test the RegEx?
0
 
S31B1Commented:
Code to do the regex below: -

string patternLenient = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
Regex reLenient = new Regex(patternLenient);

bool isLenientMatch = reLenient.IsMatch(sample);
      if (isLenientMatch)
//Do your Email based Auth
      else
//Do your Name based Auth
0
 
S31B1Commented:

Which Membership provider are you using.

If it's one of the Microsoft providers, I don't think it has support for calling Membership.ValidateUser(byEmail)

if this is the case you will have to impliment your own membership Provider.

This is not as scary as it sounds though See Here for an option to get you started: -

http://download.microsoft.com/download/5/5/b/55bc291f-4316-4fd7-9269-dbf9edbaada8/sampleaccessproviders.vsi
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.