24

People I need help I am in a new project in which I am implementing identity server 4 and I am trying to recover the previously created users with asp.Identity that I have in my database to be able to verify when I make an external login from identity server 4. Sorry my english. My Startup.cs

public class Startup
{    

public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

 public void ConfigureServices(IServiceCollection services)
{
    #region --Identity ASP

    services.AddDbContext<QGoodDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")),ServiceLifetime.Transient);
    services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<QGoodDbContext>()                   
            .AddDefaultTokenProviders();

    #endregion

    services.AddMvc();

    services.AddScoped<UserManager<ApplicationUser>>();
    services.AddScoped<SignInManager<ApplicationUser>>();

    // configure identity server with in-memory stores, keys, clients and scopes
    services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients());
            //.AddAspNetIdentity<ApplicationUser>();
        }
}

When I initialize my database I already have this error

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {          

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseIdentityServer();
            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
            InitializeDatabase(app);
        }

        private void InitializeDatabase(IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
            {
                //serviceScope.ServiceProvider.GetRequiredService<QGoodDbContext>().Database.Migrate();

                var context = serviceScope.ServiceProvider.GetRequiredService<QGoodDbContext>();

            }
        }

In my AccountController i have exception when call await _userManager.FindByLoginAsync(provider, providerUserId);

 public class AccountController : Controller
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly SignInManager<ApplicationUser> _signInManager;
        private readonly QGoodDbContext _context;
        private readonly TestUserStore _users;
        private readonly IIdentityServerInteractionService _interaction;
        private readonly IClientStore _clientStore;
        private readonly IAuthenticationSchemeProvider _schemeProvider;
        private readonly IEventService _events;


        public AccountController(
            UserManager<ApplicationUser> userManager,
            SignInManager<ApplicationUser> signInManager,
            QGoodDbContext context,
            IIdentityServerInteractionService interaction,
            IClientStore clientStore,
            IAuthenticationSchemeProvider schemeProvider,
            IEventService events,
            TestUserStore users = null)
        {
            // if the TestUserStore is not in DI, then we'll just use the global users collection
            // this is where you would plug in your own custom identity management library (e.g. ASP.NET Identity)
            _users = users ?? new TestUserStore(TestUsers.Users);
            _userManager = userManager;
            _signInManager = signInManager;
            _context = context;
            _interaction = interaction;
            _clientStore = clientStore;
            _schemeProvider = schemeProvider;
            _events = events;
        }
          public async Task<IActionResult> ExternalLoginCallback()
        {
            try
            {
                // read external identity from the temporary cookie
                var result = await HttpContext.AuthenticateAsync(IdentityServer4.IdentityServerConstants.ExternalCookieAuthenticationScheme);
                if (result?.Succeeded != true)
                {
                    throw new Exception("External authentication error");
                }

                // lookup our user and external provider info
                var (userTest, provider, providerUserId, claims) = FindUserFromExternalProvider(result);
                var user = await _userManager.FindByLoginAsync(provider, providerUserId);
            }
        }
    }

Exception is:

Method not found: 'System.Reflection.MethodInfo Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.get_SelectAsyncMethod()'

Frank Culqui
  • 343
  • 2
  • 7

4 Answers4

33

Hi can you show your packages install ? I have same problem too when i use EFCore. I solved this when I installed package

Microsoft.EntityFrameworkCore.Relational >= 2.2.0
Midas
  • 564
  • 6
  • 21
Anton Makieiev
  • 439
  • 5
  • 7
  • 5
    Down-grading EF core tools didn't work. Microsoft.EntityFrameworkCore.Relational did the deed ! – Antoine Meltzheim Dec 06 '18 at 08:39
  • do not forget to upgrade database provider when upgrade EF Core – smg Jan 15 '19 at 17:24
  • 1
    Just went through the pain of this issue just to discover that I've already upvoted this answer and favorited the question lol. – Shelby115 Jan 28 '19 at 00:30
  • Worked great. Odd this isn't included by default. – Micah Osborne Aug 22 '19 at 06:26
  • An improvement suggestion: Microsoft.EntityFrameworkCore.Relational must be >= 2.2.0. I added my mistake a lower version and that does not solve the problem. When I tried with 2.2.0 it worked immediately. – Midas May 29 '20 at 10:37
  • for most of the issues, related with .net core 3.1 and .net efcore, it will be usually compatibility issue. choose the right version for efcore. – Say Jun 13 '20 at 13:36
10

There are several variations of this problem that exhibit the behavior of MissingMethodException, NotImplemementedException or MissingFieldException.

It appears these are caused by conflicting versions within the Microsoft.EntityFrameworkCore namespace. For instance, this error can be caused by referencing Microsoft.EntityFrameworkCore.InMemory 2.2.0 and Microsoft.EntityFrameworkCore.Relational 2.1.4 in the same assembly.

The solution is to harmonize the versions of nuget package references so that they do not internally reference different versions of EF Core components.

Gracie
  • 612
  • 1
  • 6
  • 13
  • It's worth mentioning some 3rd party libraries such as Devart have internal references that must also be harmonized. – Gracie Jan 08 '19 at 11:12
6

I don't know what was the real problem but after downgrade Microsoft.EntityFrameworkCore.Tools to version 2.1.4 and updgrade again to last version problem gone.

Ali Yousefi
  • 2,355
  • 2
  • 32
  • 47
0

I had this problem when I implemented my project on AWS Lambda on an .Net Core 2.2 build- I did two things to fix it:

  1. Include Microsoft.EntityFrameworkCore.Relational package from NuGet.
  2. Included the MySql.Data.EntityFrameworkCore NuGet package in the top level project - for some reason it get included in the zip file that went up to AWS, even though it was a dependency in a sub project.

I suspect it was the second action that fixed it.

Liam
  • 5,033
  • 2
  • 30
  • 39