Kentico UI site Login using Username Instead of Email

Ramakrishnan G asked on July 9, 2019 10:06

In the kentico UI site is they have login form. In the login form two textbox(Emailid and Password),

already access the site login using Email id.

Now I need to access using UserName instead of Email id. In the CMS_User table UserName column values are there. How to modify my code?

public LoginResult Login(LoginRequest loginRequest) { var user = AuthenticationHelper.AuthenticateUser(loginRequest.LoginEmail, loginRequest.Password, SiteContext.CurrentSiteName);

        if (user != null)
        {
            ChangeCookieExpiration(loginRequest.KeepLoggedIn);
            FormsAuthentication.SetAuthCookie(user.UserName, loginRequest.KeepLoggedIn);
            MembershipActivityLogger.LogLogin(user.UserName);

            return new LoginResult
            {
                LogonSuccess = true
            };
        }

        return new LoginResult
        {
            LogonSuccess = false,
            ErrorPropertyName = "loginEmail",
            ErrorMessage = ResHelper.GetString("Kadena.Logon.LogonFailed", LocalizationContext.CurrentCulture.CultureCode)
        };
    }

public class LoginRequest { public string LoginEmail { get; set; } public string UserName { get; set; } public string Password { get; set; } public bool KeepLoggedIn { get; set; }

    public ValidationFieldResult Validate()
    {
        if (string.IsNullOrWhiteSpace(LoginEmail))
        {
            return new ValidationFieldResult { Name = "loginEmail", Error = "Kadena.Logon.LoginEmailEmpty" };
        }
        if (string.IsNullOrWhiteSpace(Password))
        {
            return new ValidationFieldResult { Name = "password", Error = "Kadena.Logon.LoginEmailEmpty" };
        }
        if (!MailValidator.IsValid(LoginEmail))
        {
            return new ValidationFieldResult { Name = "loginEmail", Error = "Kadena.Logon.InvalidEmail" };
        }

        return null;
    }
}

-----------------------------------------------------

Correct Answer

Roman Hutnyk answered on July 11, 2019 08:36

You would need to hook up into authentication event, check if user got authenticated by Kentico (e.User != null) and if not (e.g. user is using email vs. his username) get username by email and try to authenticate using username and pass.

So if user uses username he will got authenticated by Kentico and your custom code won't execute, otherwise your code will attempt to 'convert' email to username and login using it.

SecurityEvents.Authenticate.Execute += OnAuthentication;

private void OnAuthentication(object sender, AuthenticationEventArgs e)
        {
            string username = SqlHelper.EscapeQuotes(e.UserName);
            string password = SqlHelper.EscapeQuotes(e.Password);

            UserInfo user = e.User;
            if (user == null)
            {
                //get user name by user email
                .....
                //attempt to authenticate by username and password
                e.User = AuthenticationHelper.AuthenticateUser("username", "password", SiteContext.CurrentSiteName);
            }
        }
1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on July 9, 2019 13:59

This all depends on how the user is created. If the user is created with a username that is am email address then it stores that email address in the username as well as the users email field. So in this case there is no reason to switch the code to use the other field.

Maybe I'm misunderstanding your question, maybe provide some more info.

Also, no need to crosspost this on SO as Kentico automatically brings those posts tagged as Kentico into the DevNet.

0 votesVote for this answer Mark as a Correct answer

Ramakrishnan G answered on July 10, 2019 09:23

In the kentico 10 site they have login box available. In the login box the current logic is able to access using Email id and Password.

My requirements is how to allow E-Mail or Username authentication at the same time.

0 votesVote for this answer Mark as a Correct answer

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