0

The error shows up when I try to sign-in the user:

public async Task<bool> SignIn(string email, string password)
{
    var user = await this._userManager.FindByEmailAsync(email);
    if (user == null)
        return false;

    if (await this._userManager.CheckPasswordAsync(user, password))
    {
        await this._signInManager.SignInAsync(user, false); //error
        return true;
    }
    return false;
}

My Startup.cs is shown below:

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        if (env.IsDevelopment())
        {
            builder.AddUserSecrets();
        }

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; set; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<RoleManager<IdentityRole>>();
        services.AddTransient<UserManager<ApplicationUser>>();
        services.AddTransient<SignInManager<ApplicationUser>>();

        // Add framework services.
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<DbContext>(options =>
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<DbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc(config =>
        {
        #if !DEBUG
            config.Filters.Add(new RequireHttpsAttribute());
        #endif
        }).AddJsonOptions(opts =>
        {
            opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        }); ;

        services.AddSingleton<IAccountService, AccountService>();
        services.AddSingleton<IAgencyService, AgencyService>();
        services.AddSingleton<IFeatureService, FeatureService>();
        ....
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");

            try
            {
                using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
                    .CreateScope())
                {
                    serviceScope.ServiceProvider.GetService<DbContext>()
                         .Database.Migrate();
                }
            }
            catch { }
        }

        app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());

        app.UseStaticFiles();

        app.UseIdentity();

        app.UseMvc(routes =>
        {
            ... routes

        });

        SeedGenerator.Initialize(app.ApplicationServices);
    }

    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
raberana
  • 11,739
  • 18
  • 69
  • 95
  • 1
    I think you should not be scoping the identity components as singleton, they should be scoped per request, not sure if that is the problem but it jumps out at me as not right. since you are not using services.AddIdentity which would call services.AddAuthentication then you should also do that and show it in your code – Joe Audette Apr 02 '16 at 12:08
  • but i called services.AddIdentity right after services.AddEntityFramework – raberana Apr 02 '16 at 12:11
  • i'll try to change the singleton part – raberana Apr 02 '16 at 12:12
  • 1
    your other calls for add rolemanager etc should be before the call to addidentity since it is also registering those components you need to get yours in there first – Joe Audette Apr 02 '16 at 12:15
  • still error :( good points though – raberana Apr 02 '16 at 12:20
  • I believe this won't help, but just to clarify, you don't need to scope the identity components at all, since you are calling app.AddIdentity() that does that among other things ([source code](https://github.com/aspnet/Identity/blob/b2bf1a46f543d289e5ca7f9538a750bf06c71534/src/Microsoft.AspNetCore.Identity/IdentityServiceCollectionExtensions.cs)) – João Pereira Apr 04 '16 at 14:10

0 Answers0