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

前端 未结 7 621
闹比i
闹比i 2021-01-07 22:05

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

相关标签:
7条回答
  • 2021-01-07 22:18

    I also had the same problem.

    I found out that services.AddIdentity<ApplicationUser, ApplicationRole>() in 2 places.

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

    0 讨论(0)
  • 2021-01-07 22:26

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

    0 讨论(0)
  • 2021-01-07 22:27

    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

    0 讨论(0)
  • 2021-01-07 22:27

    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>();
    
    0 讨论(0)
  • 2021-01-07 22:29

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

    0 讨论(0)
  • 2021-01-07 22:41

    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.

    0 讨论(0)
提交回复
热议问题