Insite Login with email/username

Insite 4.1 and up use ASP.NET identity. The ASP.NET Identity system is designed to replace the previous ASP.NET Membership and Simple Membership systems. It includes profile support, OAuth integration, works with OWIN.

If you wish to extend the default implementation to allow customers to login with their email, you can do it by

Step 1: Require unique email address. This is important as we do not want two users with the same email. You can do this by updating “RequireUniqueEmail” setting to true in App_Code/Startup.Auth.cs

 SecurityOptions.UserValidatorOptions = new UserValidatorOptions
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };

Step 2: Extend the Insite.IdentityServer.UserService class and override the AuthenticateLocalAsync method. Add a new method that searches a user by email userManager.FindByEmailAsync(email)

   public class UserServiceExtension : UserService
    {
        public UserServiceExtension(IdentityUserManager userManager)
            : base(userManager)
        {
        }

        public override async Task<AuthenticateResult> AuthenticateLocalAsync(string username, string password,
            SignInMessage message)
        {
            var user = await FindByNameOrEmailAsync(username, password);
            AuthenticateResult authenticateResult;
            if (user == null)
            {
                authenticateResult = new AuthenticateResult(MessageProvider.Current.SignInInfo_UserNamePassword_Combination);
            }
            else
            {
                if (userManager.SupportsUserLockout)
                {
                    if (await userManager.IsLockedOutAsync(user.Id))
                    {
                        authenticateResult = new AuthenticateResult(MessageProvider.Current.SignInInfo_UserLockedOut);
                        return authenticateResult;
                    }
                }
                if (await userManager.CheckPasswordAsync(user, password))
                {
                    if (userManager.SupportsUserLockout)
                        userManager.ResetAccessFailedCount(user.Id);
                    var result = await PostAuthenticateLocalAsync(user, message);
                    if (result != null)
                    {
                        authenticateResult = result;
                    }
                    else
                    {
                        var claims = await GetClaimsForAuthenticateResult(user);
                        authenticateResult = new AuthenticateResult(user.Id, await GetDisplayNameForAccountAsync(user.Id), claims);
                    }
                }
                else
                {
                    if (userManager.SupportsUserLockout)
                    {
                        var identityResult = await userManager.AccessFailedAsync(user.Id);
                    }
                    authenticateResult = new AuthenticateResult(MessageProvider.Current.SignInInfo_UserNamePassword_Combination);
                }
            }
            return authenticateResult;
        }


        public virtual async Task<IdentityUser> FindByNameOrEmailAsync
           (string usernameOrEmail, string password)
        {
            var username = usernameOrEmail;
            if (!usernameOrEmail.Contains("@"))
                return await userManager.FindAsync(username, password);
            return await userManager.FindByEmailAsync(usernameOrEmail);
    }
    }

Step 3: Finally replace Insite’s UserService with the UserServiceExtension method in App_Code/Startup.Auth.cs

            // replace registration with custom registration to allow email sign up
            SecurityOptions.IdentityServerOptions.Factory.UserService = new Registration<IUserService, UserServiceExtension>();

Run the code and you should now be able to login with your email.

Insite Login with email/username

Leave a Reply

Your email address will not be published. Required fields are marked *