Search code examples
c#asp.net-coreasp.net-core-identity

InvalidOperationException: Scheme already exists: Identity.Application


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>();
            //});
        }
    }
}

Solution

  • 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();