Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.InvalidOperationException: Scheme already exists: Identity.Application

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

  • Scheme already exists: Identity.Application #8223 seems most relevant
  • InvalidOperationException: Scheme already exists ... #1412
  • AddIdentity() fails "InvalidOperationException: Scheme already exists: Identity.Application"

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.
like image 631
Samuel Wakeman Avatar asked Oct 19 '25 03:10

Samuel Wakeman


3 Answers

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();
like image 192
A. Hasemeyer Avatar answered Oct 21 '25 16:10

A. Hasemeyer


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

_ = services.AddDefaultIdentity<IdentityUser>(options =>
  options.SignIn.RequireConfirmedAccount = true)
         .AddEntityFrameworkStores<ApplicationDbContext>();
like image 38
Alberto Cláudio Mandlate Avatar answered Oct 21 '25 18:10

Alberto Cláudio Mandlate


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();
like image 45
A. Hasemeyer Avatar answered Oct 21 '25 16:10

A. Hasemeyer