Login with either of the two columns

Ashutosh Pandey asked on March 14, 2020 18:37

We have two columns, primary email and business email. While registration, we are setting primary email as the username.

Is it possible to login either primary email or business email?

Currently we are using this code:

UserInfo userInfo = AuthenticationHelper.AuthenticateUser(txtEmailAddress.Text, txtPassword.Text, SiteContext.CurrentSiteName);

Correct Answer

Peter Mogilnitski answered on March 15, 2020 00:14

Well you technically can, but why? Username and password is a unique pair. Are you controlling uniqueness of the business email? if you don't - you might have the same business email for different usernames.

If biz email is unique (like sql constraint) you probably can do something simple like this:

UserInfo ui = UserInfoProvider.GetUsers()
            .Where(new WhereCondition()
                .WhereEquals("BusinessEmail", txtEmailAddress.Text)
                .WhereEquals("UserName", txtEmailAddress.Text)
var userName = ui?.UserName;
UserInfo userInfo = AuthenticationHelper.AuthenticateUser(userName , txtPassword.Text, SiteContext.CurrentSiteName);

But you have to take care of password reset functionality as well

3 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.