Skip to content

PasswordSignInAsync with duplicate emails Sequence contains more than one element exception #14368

@dotnetshadow

Description

@dotnetshadow

Describe the bug

I have an asp.net core 3.0 app and I wanted to be able to sign in using a username instead of an email address. Because I'm importing a legacy system there could be a chance that multiple usernames share the same email address.

Using the default identity razor scaffold when looking at the login code:
var result = await _signInManager.PasswordSignInAsync(username, Input.Password, Input.RememberMe, lockoutOnFailure: true);

If I enter a username and incorrect password I get the following error:
InvalidOperationException: Sequence contains more than one element

It seems I can't login anymore with users that share duplicate email addresses?

To Reproduce

Steps to reproduce the behavior:

  1. Create an asp.net core 3.0 app with identity
  2. Scaffold the identity
  3. Add a couple of users with different usernames belonging to the same email address
  4. Try to login and enter the wrong password

Expected behavior

It should return a failed result instead of Sequence contains more than one element

Screenshots

image

Additional context

InvalidOperationException: Sequence contains more than one element

    System.Linq.ThrowHelper.ThrowMoreThanOneElementException()
    System.Linq.Enumerable.SingleOrDefault<TSource>(IEnumerable<TSource> source)
    Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<TResult>(Expression query)
    Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<TResult>(Expression expression)
    System.Linq.Queryable.SingleOrDefault<TSource>(IQueryable<TSource> source)
    Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore<TUser, TRole, TContext, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken, TRoleClaim>.FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken)
    Microsoft.AspNetCore.Identity.UserManager<TUser>.FindByEmailAsync(string email)
    Microsoft.AspNetCore.Identity.UserValidator<TUser>.ValidateEmail(UserManager<TUser> manager, TUser user, List<IdentityError> errors)
    Microsoft.AspNetCore.Identity.UserValidator<TUser>.ValidateAsync(UserManager<TUser> manager, TUser user)
    Microsoft.AspNetCore.Identity.UserManager<TUser>.ValidateUserAsync(TUser user)
    Microsoft.AspNetCore.Identity.UserManager<TUser>.UpdateUserAsync(TUser user)
    Microsoft.AspNetCore.Identity.UserManager<TUser>.AccessFailedAsync(TUser user)
    Microsoft.AspNetCore.Identity.SignInManager<TUser>.CheckPasswordSignInAsync(TUser user, string password, bool lockoutOnFailure)
    Microsoft.AspNetCore.Identity.SignInManager<TUser>.PasswordSignInAsync(TUser user, string password, bool isPersistent, bool lockoutOnFailure)
    Foo.Web.Areas.Identity.Pages.Account.LoginModel.OnPostAsync(string returnUrl) in Login.cshtml.cs

                    var result = await _signInManager.PasswordSignInAsync(user, Input.Password, Input.RememberMe, lockoutOnFailure: true);

Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory+GenericTaskHandlerMethod.Convert<T>(object taskAsObject)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory+GenericTaskHandlerMethod.Execute(object receiver, object[] arguments)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()

asp.net core 3.0
#4539 (comment)

Metadata

Metadata

Assignees

Labels

area-identityIncludes: Identity and providers

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions