Reputation: 2871
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)
Upvotes: 53
Views: 68758
Reputation: 1545
I had to use one of these, not 2 or more of them, make sure there is no duplication in your program.cs regarding these :
Upvotes: 0
Reputation: 61
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.
Upvotes: 6
Reputation: 1185
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.
Upvotes: 112
Reputation: 2000
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();
Upvotes: 8
Reputation: 78
I came across this question and I had the same issue after I scaffolded a new razor page Login and Register
what I have found is a new file with the following name created "IdentityHostingStartup" and has the following code :
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
services.AddDbContext<IdentityExplorationContext>(options =>
options.UseSqlServer(
context.Configuration.GetConnectionString("IdentityExplorationContextConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<IdentityExplorationContext>();
});
}
and the problem here is we have AddDefaultIdentity duplicated in two different files if you remove the one from the newly created file after the scaffolding step your app will work fine again
so the function above should be as follows :
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
services.AddDbContext<IdentityExplorationContext>(options =>
options.UseSqlServer(
context.Configuration.GetConnectionString("IdentityExplorationContextConnection")));
});
}
this worked for me
hope this helps, thanks
Upvotes: 0
Reputation: 1645
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.
Upvotes: 21
Reputation: 1
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>();
Upvotes: 0
Reputation: 1304
I had the same issue, I was registering services in two different files in the same time: Startup.cs and IdentityHostingStartup.cs files.
I sepeprated registering the services in both files as following:
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")
)
);
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>();
Upvotes: 3
Reputation: 1774
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...
Upvotes: 3
Reputation: 6017
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>();
Upvotes: 3
Reputation: 2871
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
Upvotes: 1