Aspnetcore.docs: Scheme already exists: Identity.Application

Created on 22 Aug 2018  ·  10Comments  ·  Source: dotnet/AspNetCore.Docs

Have followed Create full identity UI source but am now receiving an error - “InvalidOperationException: Scheme already exists: Identity.Application” - when running solution. Startup.cs below;


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Most helpful comment

Not sure if this is a bug, an issue with doc or just I've miss-understood, however believe I have solved my problem.

IdentityHostingStartup.cs contained;

services.AddDefaultIdentity<IdentityUser>() .AddEntityFrameworkStores<myProjectContext>();

have commented this out and it seems to be working so far.
Doc: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.1&tabs=visual-studio

All 10 comments

Not sure if this is a bug, an issue with doc or just I've miss-understood, however believe I have solved my problem.

IdentityHostingStartup.cs contained;

services.AddDefaultIdentity<IdentityUser>() .AddEntityFrameworkStores<myProjectContext>();

have commented this out and it seems to be working so far.
Doc: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.1&tabs=visual-studio

How do you configure the RequiredConfirmEmail and RequiredConfirmedPhoneNumber when this service code is commented out?

@jimgit I think what he was saying was he had BOTH services.AddDefaultIdentity and services.AddIdentity defined in the same startup file.

Well that's what I had :-)

In my case I had AddDefaultIdentity in Startup and IdentityHostingStartup
I think problem in scaffolding. I created project and after that I scaffold Identity and had 2 registration AddDefaultIdentity

Thanks for contacting us. We believe that the question you've raised have been answered. If you still feel a need to continue the discussion, feel free to reopen it and add your comments.

For others.

I store my context in another project (which is probably the reason it did not get populated in the drop down for creating the identity).

So I removed the following line from IdentityHostingStartup.cs

 services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<CreatedContext>();

and updated the Startup.cs file with

services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<CreatedContext>();

I did´t notice right away that the CreatedContext derived from IdentityDbConext

 public class CreatedContext : IdentityDbContext<IdentityUser>

i have a similar issue but If i comment out that line that creates the identity I get no identy created but if I keep it I create 2 . I don't understand how this is possible. here is my startup.cs

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApp1.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace WebApp1
{
public class Startup
{
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 think its scoffoling problem that add these line in IdentityHostingStartup.cs without checking services
please check ! @Rick-Anderson
run this :

dotnet new mvc -o MvcAuth -au Individual
code -r MvcAuth
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet aspnet-codegenerator identity -dc MyWeb.Data.ApplicationDbContext

now you have the problem

Sturlath, this worked for me as well...does anyone know if there will be any issues if

services.AddDefaultIdentity()
.AddEntityFrameworkStores();

are removed from the IdentityHostingStartup.cs file or is this required in a different way?

For reference, I had the same issue when using services.AddDefaultIdentity() with services.AddAuthentication().AddIdentityCookies().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fabich picture fabich  ·  3Comments

Rick-Anderson picture Rick-Anderson  ·  3Comments

danroth27 picture danroth27  ·  3Comments

Rick-Anderson picture Rick-Anderson  ·  3Comments

madelson picture madelson  ·  3Comments