'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 1:[1]
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.
Inside the class, another instance of "AddDefaultIdentity" is created along with a few other services.
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.
Solution 2:[2]
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.
Solution 3:[3]
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...
Solution 4:[4]
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>();
Solution 5:[5]
This is a change from .Net Core 2.0->2.1, I guess the guide hasnt been updated.
After stumbling upon this SO post I : Removed the lines entire services.AddIdentity()...call (all 3 lines) (but of course kept the AddDefaultIdentity()-call that was there before
Changed back in ApplicationDbContext.cs from
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
to
public class ApplicationDbContext : IdentityDbContext<IdentityUser>
... So if your starting out from scratch (new .Net Core 2.1-template), all you have to do is add lines
services.AddAuthentication().AddFacebook(facebookOptions =>
{
facebookOptions.AppId = Configuration["...FacebookLogin:AppId"];
facebookOptions.AppSecret = Configuration["...FacebookLogin:ClientSecret"];
});
from the tutorial.
At least this "fix" takes me through so far that the users can register, havent investigated where my "ApplicationUser" went (in case/when I want to add more user-properties)...since there is no reference to it anymore
Solution 6:[6]
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>();
Solution 7:[7]
I ran into the same problem, with my plugin Kestrel web server project. I have a plugin using Microsoft Identity, and when I use that plugin two times, I get the "InvalidOperationException: Scheme already exists: Identity.Application" exception.
As mentioned earlier in this thread, the error comes from calling "services.AddIdentity" more then one time. In my project once for each time the plugin is used.
Anyway the solution was to replace:
services.AddIdentity<IdentityUser, IdentityRole>((options) => { ... })....
With:
services.AddIdentityCore<IdentityUser>((options) => { ... }).AddRoles<IdentityRole>()....
The latter apparently catches the exception.
Solution 8:[8]
if removing the "addDefaultIdentity" from "Startup.cs" does not work for you .
Replace services.AddDefaultIdentity<ApplicationUser>
to AddIdentity<ApplicationUser, IdentityRole>
/*
services.AddIdentity<ApplicationUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
*/
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
Solution 9:[9]
Under .Net 6, go to program.cs and comment dupplicated 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();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tidy |
Solution 2 | Greg Gum |
Solution 3 | Pang |
Solution 4 | eyllanesc |
Solution 5 | Cowborg |
Solution 6 | Diego Venâncio |
Solution 7 | ˈvɔlə |
Solution 8 | eyllanesc |
Solution 9 | Matias Masso |