1

I have tried the solution in this question without any luck.

In my Startup.cs I have this:

services.AddDbContext<ApplicationDbContext>(options => 
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

//services.AddIdentity<ApplicationUser, ApplicationRole>(options => options.SignIn.RequireConfirmedAccount = true)
//    .AddRoles<ApplicationRole>()
//    .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity<ApplicationUser, ApplicationRole>()
    .AddRoles<ApplicationRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

services.Configure<IdentityOptions>(options =>
{
    options.SignIn.RequireConfirmedAccount = true;
    options.Password.RequiredLength = 8;
    options.Password.RequireDigit = false;
    options.Password.RequireUppercase = false;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequiredUniqueChars = 5;
    options.Lockout.MaxFailedAccessAttempts = 3;
    options.Lockout.DefaultLockoutTimeSpan = new TimeSpan(0, 15, 0); // 15 minutes
});

I tried removing the file IdentityHostingStartup.cs from my solution, and that didn't make any difference. Now I have added it back (in Areas/Identity/Pages), but commented out what I thought was causing the trouble:

//[assembly: HostingStartup(typeof(MyAppName.Areas.Identity.IdentityHostingStartup))]
namespace MyAppName.Areas.Identity
{
    public class IdentityHostingStartup : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder)
        {
            //builder.ConfigureServices((context, services) => {
            //    services.AddDbContext<ApplicationDbContext>(options =>
            //        options.UseSqlServer(
            //            context.Configuration.GetConnectionString("GroupManagerContextConnection")));

            //services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
            //    .AddEntityFrameworkStores<ApplicationDbContext>();
            //});
        }
    }
}
Stian
  • 1,522
  • 2
  • 22
  • 52
  • Be sure your ApplicationDbContext is somthing like `ApplicationDbContext:IdentityDbContext`. Also the ApplicationUser should inherit IdentityUser and ApplicationRole inherit IdentityRole. – Rena Jun 21 '21 at 08:53

1 Answers1

1

This ASP Identity Core InvalidOperationException is thrown mostly when a duplicate call to function exists in Startup.cs. Now please see your code services.AddIdentity<ApplicationUser, ApplicationRole>() .AddRoles<ApplicationRole>() I think here you called roles two times.for that you found this exception.you should use :-

services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();
            

Or

services.AddIdentity<ApplicationUser>()
    .AddRoles<ApplicationRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

So, Above I described your main problem.

Now you can try something like this:-

public class ApplicationUser : IdentityUser
{
    //clarify code
}

Startup.cs

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

Pritom Sarkar
  • 2,154
  • 3
  • 11
  • 26
  • 1
    Thanks! As it turned out, I did actually have a second call to `AddIdentity` a bit further down in my `Startup.cs`... Silly mistake. – Stian Jun 21 '21 at 14:58