Home > Software design >  Crash after upgrading from IdentityServer4 to Dundee 6.2 - Sequence contains more than one element
Crash after upgrading from IdentityServer4 to Dundee 6.2 - Sequence contains more than one element

Time:01-05

I'm upgrading an existing project from .Net Core 2.2 & IdentityServer4 2.4 to .Net 7 and Duende 6.2.0. I performed this update incrementally, by upgrading to IdentityServer4 3.0, then 4.0, and running migrations on both PersistedGrantDbContext and ConfigurationDbContext with each major version of IdentityServer4.

After I made it to IdentityServer4 v4, I then migrated to Duende 6.2 and reran migrations one final time.

Upon calling the connect/token endpoint, I am greeted with the following exception:

System.InvalidOperationException: Sequence contains more than one element.
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Identity.UserManager`1.FindByEmailAsync(String email)
   at Microsoft.AspNetCore.Identity.UserValidator`1.ValidateEmail(UserManager`1 manager, TUser user, List`1 errors)
   at Microsoft.AspNetCore.Identity.UserValidator`1.ValidateAsync(UserManager`1 manager, TUser user)
   at Microsoft.AspNetCore.Identity.UserManager`1.ValidateUserAsync(TUser user)
   at Microsoft.AspNetCore.Identity.UserManager`1.UpdateUserAsync(TUser user)
   at Microsoft.AspNetCore.Identity.UserManager`1.CheckPasswordAsync(TUser user, String password)
   at Microsoft.AspNetCore.Identity.SignInManager`1.CheckPasswordSignInAsync(TUser user, String password, Boolean lockoutOnFailure)
   at Duende.IdentityServer.AspNetIdentity.ResourceOwnerPasswordValidator`1.ValidateAsync(ResourceOwnerPasswordValidationContext context) in /_/src/AspNetIdentity/ResourceOwnerPasswordValidator.cs:line 52
   at Duende.IdentityServer.Validation.TokenRequestValidator.ValidateResourceOwnerCredentialRequestAsync(NameValueCollection parameters) in /_/src/IdentityServer/Validation/Default/TokenRequestValidator.cs:line 500
   at Duende.IdentityServer.Validation.TokenRequestValidator.RunValidationAsync(Func`2 validationFunc, NameValueCollection parameters) in /_/src/IdentityServer/Validation/Default/TokenRequestValidator.cs:line 189
   at Duende.IdentityServer.Validation.TokenRequestValidator.ValidateRequestAsync(NameValueCollection parameters, ClientSecretValidationResult clientValidationResult) in /_/src/IdentityServer/Validation/Default/TokenRequestValidator.cs:line 174
   at Duende.IdentityServer.Endpoints.TokenEndpoint.ProcessTokenRequestAsync(HttpContext context) in /_/src/IdentityServer/Endpoints/TokenEndpoint.cs:line 98
   at Duende.IdentityServer.Endpoints.TokenEndpoint.ProcessAsync(HttpContext context) in /_/src/IdentityServer/Endpoints/TokenEndpoint.cs:line 75
   at Duende.IdentityServer.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IdentityServerOptions options, IEndpointRouter router, IUserSession userSession, IEventService events, IIssuerNameService issuerNameService, ISessionCoordinationService sessionCoordinationService) in /_/src/IdentityServer/Hosting/IdentityServerMiddleware.cs:line 101
   at Duende.IdentityServer.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IdentityServerOptions options, IEndpointRouter router, IUserSession userSession, IEventService events, IIssuerNameService issuerNameService, ISessionCoordinationService sessionCoordinationService) in /_/src/IdentityServer/Hosting/IdentityServerMiddleware.cs:line 117
   at Duende.IdentityServer.Hosting.MutualTlsEndpointMiddleware.Invoke(HttpContext context, IAuthenticationSchemeProvider schemes) in /_/src/IdentityServer/Hosting/MutualTlsEndpointMiddleware.cs:line 94
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Duende.IdentityServer.Hosting.DynamicProviders.DynamicSchemeAuthenticationMiddleware.Invoke(HttpContext context) in /_/src/IdentityServer/Hosting/DynamicProviders/DynamicSchemes/DynamicSchemeAuthenticationMiddleware.cs:line 47
   at Duende.IdentityServer.Hosting.BaseUrlMiddleware.Invoke(HttpContext context) in /_/src/IdentityServer/Hosting/BaseUrlMiddleware.cs:line 27
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Startup.cs

 services.AddIdentityServer()
         .AddConfigurationStore(options =>
         {
            options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
         })
         .AddOperationalStore(options =>
         {
             options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));

             options.EnableTokenCleanup = true;
             options.TokenCleanupInterval = 30;
         })
         .AddInMemoryApiScopes(IdentityServerConfig.GetApiScopes())
         .AddAspNetIdentity<ApplicationUser>()
         .AddExtensionGrantValidator<TokenGrantValidator>()
         .AddProfileService<ProfileService>();

Relevant Packages

    <PackageReference Include="Duende.IdentityServer" Version="6.2.0" />
    <PackageReference Include="Duende.IdentityServer.AspNetIdentity" Version="6.2.0" />
    <PackageReference Include="Duende.IdentityServer.EntityFramework" Version="6.2.0" />
    <PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
  
    <PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="7.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.TagHelpers" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.1" />

and, in another referenced project

    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.1" />
    <PackageReference Include="NLog" Version="5.1.0" />
    <PackageReference Include="SqlBulkTools.NetStandard" Version="2.1.30" />
    <PackageReference Include="StackExchange.Redis" Version="2.1.30" />
    <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />

I understand the error Sequence contains more than one element, but since this exception occurs within the middleware, I'm unsure how to resolve this.

Question

How can I go about debugging/troubleshooting an exception that is occurring within an SDK or middleware?

CodePudding user response:

From the stack trace, it seems that this method is the one causing you the trouble:

at Microsoft.AspNetCore.Identity.UserManager`1.FindByEmailAsync(String email)

I would check the database and check if you have multiple users with the same email.

  • Related