[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 527
  • Last Modified:

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
0
mugsey
Asked:
mugsey
  • 5
1 Solution
 
Jens FiedererCommented:
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now