Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 684
Cowborg Avatar asked Jul 03 '18 19:07

Cowborg


6 Answers

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.

Hope this helps.

like image 111
Tidy Avatar answered Nov 20 '22 16:11

Tidy


I also had the same problem.

I found out that services.AddIdentity<ApplicationUser, ApplicationRole>() had been added when scaffolding Identity, so it was now in 2 places in Program.cs/StartUp.cs (depending on which version of .net core you are using.)

I removed one of them and now everything works as expected.

like image 16
Hamid Noahdi Avatar answered Nov 20 '22 16:11

Hamid Noahdi


For me (ASP.NET Core v2), I had:

services.AddIdentity<MyUser, MyRole>()
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddUserStore<MyUserStore>()
                    .AddRoleStore<MyRoleStore>()
                    .AddRoleManager<MyRoleManager>()
                    .AddDefaultTokenProviders();

in Startup.cs. And when I scaffolded Identity, it added IdentityHostingStartup.cs, and I had copy/pasted another similar but default block in based on some email sending code:

builder.ConfigureServices((context, services) =>
                {
                    services.AddDefaultIdentity<IdentityUser>(config =>
                    {
                        config.SignIn.RequireConfirmedEmail = false;//TODO:
                    })
                    .AddEntityFrameworkStores<ApplicationDbContext>();
                });

So I moved ALL Identity config into IdentityHostingStartup.cs (ie only 1 configure!!!) it worked as expected...

like image 3
James Joyce Avatar answered Nov 20 '22 16:11

James Joyce



I had the same issue, I was registering services in two different files in the same time: Startup.cs and IdentityHostingStartup.cs files.

What I did?

I sepeprated registering the services in both files as following:

IdentityHostingStartup.cs

I only registered Identity DB Context in this file:

//lets register identity db context
builder.ConfigureServices((context, services) => {
    services.AddDbContext<IdentityDbContext>(options =>
    options.UseSqlServer(
        context.Configuration.GetConnectionString("IdentityDBConnection"),
        x => x.MigrationsAssembly("WebApplication1")
        )
    );

Startup.cs

In this file I registered DefaultIdentity, Roles and EntityFrameworkstores

//I registered the DefaultIdentity, the Roles and the EntityFrameworkstores
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddRoles<IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
like image 3
Hamza Dahmoun Avatar answered Nov 20 '22 15:11

Hamza Dahmoun


In my case, problem was after add:

services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();

confliting with below code, in Startup.cs:

services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyConnectionString")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
like image 2
Diego Venâncio Avatar answered Nov 20 '22 15:11

Diego Venâncio


Under .Net 6, go to program.cs and comment duplicated connectionString, AddDbContext and AddDefaultIdentity:

var builder = WebApplication.CreateBuilder(args);
//var connectionString = builder.Configuration.GetConnectionString("ApplicationDbContextConnection") ?? throw new InvalidOperationException("Connection string 'ApplicationDbContextConnection' not found.");

//builder.Services.AddDbContext<ApplicationDbContext>(options =>
//    options.UseSqlServer(connectionString));;

//builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
//    .AddEntityFrameworkStores<ApplicationDbContext>();;

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
builder.Services.AddSingleton<WeatherForecastService>();

var app = builder.Build();
like image 2
Matias Masso Avatar answered Nov 20 '22 14:11

Matias Masso