3

I wanted to add my own custom data for users so I was following the tutorial Add, download, and delete custom user data to Identity in an ASP.NET Core project

I already had an existing application so I could not follow that tutorial line by line (my existing application has a database of users already). I didn't get very far in it when I hit the error: System.InvalidOperationException: Scheme already exists: Identity.Application

I used the scaffolder to try to add ... (? the code ?)

I've gone through the links below but to no avail

It seems like a lot of other people add problems with calling the identity twice but I'm only calling it once in my code. I've also tried commenting out the line entirely in the startup and then it says there is none defined and gets mad at me. I've also tried switch form the default as shown below.

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

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

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

            services.AddIdentity<IdentityUser, IdentityRole>()
       // services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<WebApp1.Models.WebApp1Context>()
            .AddDefaultTokenProviders();


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAuthentication();

            app.UseMvc();
        }

I feel like I shouldn't be getting the exception thrown and yet.... any advice on a fix?

edit: relevant steps i took until i got this error.

  • Create project to use invidual user accounts in process creation
  • override with scaffolder,
  • and create a secondary user model that you can override.
  • migrate and update database run.
surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Sagick
  • 317
  • 2
  • 6
  • 20
  • Is the class `IdentityUser` in `services.AddIdentity()` your class or the `IdentityUser` class that is part of AspNetIdentity? If it is you need to declare that specifically, otherwise AspNetIdentity is thinking you are trying to declare IdentityUser as it's own IdentityUser Class which is throwing the exception. – A. Hasemeyer Jun 03 '19 at 20:55
  • Another scenario: You used the Identity Scaffold to auto-generate over a partially built authentication setup. In this case, you'll see the macro created your user in Areas\Identity.... – LargeDachshund Jun 08 '20 at 23:28
  • Taken from [this answer](https://stackoverflow.com/a/58331262/819887): If you remove the "addDefaultIdentity" from your "Startup.cs" / "Program.cs" your app should start. This worked for me with .Net 6. – surfmuggle Jun 21 '22 at 13:30

4 Answers4

3

Try renaming your IdentityUser class to something unique from AspNetIdentity classes. Then make sure you are inheriting from IdentityUser

For example here is a class

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool IsDisabled { get; set; }
}

And this is the startup

services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<IdentityContext>()
            .AddDefaultTokenProviders();
A. Hasemeyer
  • 1,452
  • 1
  • 18
  • 47
  • https://stackoverflow.com/questions/51161729/addidentity-fails-invalidoperationexception-scheme-already-exists-identity has the correct answer. – Ogbonna Vitalis Dec 18 '19 at 23:49
2

The marked answer is not the correct answer. I had the same issue and the reason is that when adding ALL identity scaffolding (via dotnet aspnet-codegenerator as Visual Studio crashes ),it creates a IdentityHostingStartup class under areas/identity. This class duplicates the identity setup in startup.cs. So deleting this class fixed the problem.

user1029883
  • 695
  • 7
  • 21
2

I had the same error, but the problem was that I called it twice:

_ = services.AddDefaultIdentity<IdentityUser>(options =>
  options.SignIn.RequireConfirmedAccount = true)
         .AddEntityFrameworkStores<ApplicationDbContext>();
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
0

This ASP Identity Core InvalidOperationException is thrown mostly, when a duplicate call to function exists in Startup.cs or any class in the project using ASP Identity Core, what is required for ASP Identity to work in Startup class:

public void ConfigureServices(IServiceCollection services)
{
   _ = services.AddDbContext<ApplicationDbContext>(options =>                
  options.UseSqlServer(Configuration.GetConnectionString("AmpCoreDb")));

   _ = services.AddDefaultIdentity<IdentityUser>(options =>
  options.SignIn.RequireConfirmedAccount = true)
         .AddEntityFrameworkStores<ApplicationDbContext>();

  _ = services.AddScoped<AuthenticationStateProvider, 
  RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();

  _ = services.AddDatabaseDeveloperPageExceptionFilter();

}
Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
  • For anyone searching [`Startup.cs` was removed in .NET 6](https://stackoverflow.com/questions/70952271/startup-cs-class-is-missing-in-net-6) – surfmuggle Jun 21 '22 at 13:14