SignInManager - Sign In With Email

Sep 26, 2014 at 12:16 AM
Edited Sep 26, 2014 at 12:16 AM
I want to be able to log my users in using an email address as in many modern sites (i.e. FB). People are more likely to remember their email than their user name allot of the time.

Below is a naive implementation. Maybe a more flexible way would be to allow us to pass in a Func with a method for getting the user?
public virtual async Task<SignInStatus> PasswordEmailSignInAsync(string emailAddress, string password, bool isPersistent, bool shouldLockout)
        {
            if (UserManager == null)
            {
                return SignInStatus.Failure;
            }
            var user = await UserManager.FindByEmailAsync(emailAddress).WithCurrentCulture();
            if (user == null)
            {
                return SignInStatus.Failure;
            }
            if (await UserManager.IsLockedOutAsync(user.Id).WithCurrentCulture())
            {
                return SignInStatus.LockedOut;
            }
            if (await UserManager.CheckPasswordAsync(user, password).WithCurrentCulture())
            {
                return await SignInOrTwoFactor(user, isPersistent).WithCurrentCulture();
            }
            if (shouldLockout)
            {
                // If lockout is requested, increment access failed count which might lock out the user
                await UserManager.AccessFailedAsync(user.Id).WithCurrentCulture();
                if (await UserManager.IsLockedOutAsync(user.Id).WithCurrentCulture())
                {
                    return SignInStatus.LockedOut;
                }
            }
            return SignInStatus.Failure;
        }
Going to extend it for now but be nice to have this feature baked in.

Cheers

Steve
Developer
Oct 1, 2014 at 8:19 PM
You can use the email=username and this feature would work with built in apis. Do you want to keep the username and email separate ?
Oct 2, 2014 at 10:52 PM
Hi Suhasj, thanks for getting back.

That's exactly what I mean. I have a site with both user names such as suhasj and SketchySteve but want to record their email addresses too. In this instance I would like them to be able to sign in with their emails.

Maybe you could bake in a more extensible way of doing it, maybe if their was an option for a Fnc returning a user.

Func<User, bool>.

We could then just chuck in a delegate.

Cheers

Steve
Developer
Oct 3, 2014 at 8:40 PM
I'll take this up with the team. Meanwhile you could override the PasswordSignin method to get the user from the email and then pass it to the base.PasswordSignin(user.username, password)
 public override async Task<SignInStatus> PasswordEmailSignInAsync(string email, string password, bool isPersistent, bool shouldLockout)
{
 var user = async UserManager.FindByEmailAsync(email);
 return PasswordSignInAsync(user.UserName,password,isPersistent,shouldLockout);
}
Oct 6, 2014 at 12:55 AM
Cheers suhasj,

Think it would be useful to have that feature, in the mean time that's allot cleaner than my copy and paste job. Will implement it that way for now.

Thanks