asp.net-identityasp.net-core-2.0

AddIdentity() fails "InvalidOperationException: Scheme already exists: Identity.Application"


I'm trying to add facebook login to my .NET Core 2.1 site

I'm following this , guide and more specific, this (for facebook login)

After have adding the lines below to startup.cs, inside ConfigureServices-method

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
    ...
}

I get the error message, when I'm running the application. Without these lines it works "ok". The user can login to facebook and approve my application and I get email and what not. BUT I'm guessing the user information will not be added to my database

InvalidOperationException: Scheme already exists: Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(string name, Action configureBuilder)...

The code goes past the added lines and the error appears after a short while (during startup). I have searched in my project (which is just a bare .NET Core 2.1 Web application, from template) and I cant see any other usages of "AddIdentity".

I did find a "AddDEfaultIdentity()", commented that line out. but then I got

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered. Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)


Solution

  • I had a similar issue. This might be more helpful for people using .Net Core 3.0. After digging around I found out that once you create an "Identity" area using scaffolding. A file called "IdentityHostingStartup.cs" is created inside the Identity folder.

    IdentityHostingStartup Location

    Inside the class, another instance of "AddDefaultIdentity" is created along with a few other services.

    IdentityHostingStartup File View

    If you remove the "addDefaultIdentity" from your "Startup.cs" your app should start. Also, If you are getting a null connection string error. Update the connection string inside of the IdentityHostintgStartup.cs

    Note: Deleting either of the addDefaultIdentities will work. You just can't have it in both locations.