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

隐身守侯 提交于 2019-12-23 07:39:13

问题


Im trying to add facebook login to my .NET Core 2.1 site

Im 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 Im 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 Im 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)


回答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.




回答2:


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




回答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...




回答4:


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>();


来源:https://stackoverflow.com/questions/51161729/addidentity-fails-invalidoperationexception-scheme-already-exists-identity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!