【问题标题】:AddIdentity() fails "InvalidOperationException: Scheme already exists: Identity.Application"AddIdentity() 失败“InvalidOperationException:方案已存在:Identity.Application”
【发布时间】:2018-12-12 04:33:46
【问题描述】:

我正在尝试将 facebook 登录添加到我的 .NET Core 2.1 站点

我正在关注 this ,指导和更具体的,this (for facebook login)

将下面的行添加到startup.cs,在ConfigureServices-method中

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
    ...
}

我在运行应用程序时收到错误消息。没有这些行,它可以“正常”工作。用户可以登录 facebook 并批准我的申请,然后我会收到电子邮件等等。但我猜用户信息不会添加到我的数据库中

InvalidOperationException:方案已存在:Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(字符串 名称,操作 configureBuilder)...

代码超出了添加的行,并且在片刻后出现错误(在启动期间)。我在我的项目中进行了搜索(这只是一个裸露的 .NET Core 2.1 Web 应用程序,来自模板),但我看不到“AddIdentity”的任何其他用法。

我确实找到了一个“AddDEfaultIdentity()”,注释掉了那一行。但后来我得到了

InvalidOperationException:类型无服务 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' 已注册。 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)

【问题讨论】:

    标签: asp.net-identity asp.net-core-2.0


    【解决方案1】:

    我遇到了类似的问题。这可能对使用 .Net Core 3.0 的人更有帮助。在四处挖掘之后,我发现一旦你使用脚手架创建了一个“身份”区域。在 Identity 文件夹中创建了一个名为“IdentityHostingStartup.cs”的文件。

    在类中,“AddDefaultIdentity”的另一个实例与其他一些服务一起创建。

    如果您从“Startup.cs”中删除“addDefaultIdentity”,您的应用应该会启动。此外,如果您收到空连接字符串错误。更新 IdentityHostintgStartup.cs 中的连接字符串

    注意:删除任一 addDefaultIdentities 都可以。你只是不能在这两个地方都有它。

    希望这会有所帮助。

    【讨论】:

    • 如果您使用的是 .Net Core 3.0 这就是解决方案。
    • 这才是真正的解决方案
    • 我只是在阅读答案后哈哈大笑......我真的期待脚手架以某种方式更新启动,但类保持不变......最小惊喜的原则完全没有窗户。
    • 不要忘记检查,如果您从现有的 Identity 构建脚手架,其中通常 ApplicationUser 是从 IdentityUser 派生的,那么您使用相同的身份模型。
    • in .Net Core 5.0 你应该从 StartUp 中删除这部分“services.AddDefaultIdentity
    【解决方案2】:

    我也遇到了同样的问题。

    我在 2 个地方发现了 services.AddIdentity&lt;ApplicationUser, ApplicationRole&gt;()

    我删除了其中一个,现在一切正常。

    【讨论】:

      【解决方案3】:

      对我来说(ASP.NET Core v2),我有:

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

      在 Startup.cs 中。当我搭建 Identity 脚手架时,它添加了 IdentityHostingStartup.cs,并且我根据一些电子邮件发送代码复制/粘贴了另一个类似但默认的块:

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

      所以我将所有身份配置移至 IdentityHostingStartup.cs(即只有 1 个配置!!!)它按预期工作......

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题,我同时在两个不同的文件中注册服务:Startup.cs 和 IdentityHostingStartup.cs 文件。

        我做了什么?

        我在两个文件中分别注册服务如下:

        IdentityHostingStartup.cs

        我只在这个文件中注册了身份数据库上下文:

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

        在这个文件中我注册了 DefaultIdentity、Roles 和 EntityFrameworkstores

        //I registered the DefaultIdentity, the Roles and the EntityFrameworkstores
        services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddRoles<IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
        

        【讨论】:

          【解决方案5】:

          这是.Net Core 2.0->2.1的变化,我猜指南还没有更新。

          偶然发现this SO post后: 删除了整个 services.AddIdentity()...call 的行(所有 3 行)(但当然保留了之前的 AddDefaultIdentity()-call

          改回 ApplicationDbContext.cs
          public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
          

          public class ApplicationDbContext : IdentityDbContext<IdentityUser>
          

          ...因此,如果您从头开始(新的 .Net Core 2.1 模板),您所要做的就是添加行

               services.AddAuthentication().AddFacebook(facebookOptions =>
                  {
                      facebookOptions.AppId = Configuration["...FacebookLogin:AppId"];
                      facebookOptions.AppSecret = Configuration["...FacebookLogin:ClientSecret"];
                  });
          

          来自教程。

          至少这个“修复”让我到目前为止用户可以注册,还没有调查我的“ApplicationUser”去了哪里(以防万一/当我想添加更多用户属性时)......因为没有参考对它了

          【讨论】:

            【解决方案6】:

            就我而言,问题是在添加之后:

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

            Startup.cs中的以下代码冲突:

            services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MyConnectionString")));
            services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();
            

            【讨论】:

              【解决方案7】:

              我的插件 Kestrel Web 服务器项目也遇到了同样的问题。 我有一个使用 Microsoft Identity 的插件,当我使用该插件两次时,我得到“InvalidOperationException:Scheme 已经存在:Identity.Application”异常。

              正如本主题前面所述,错误来自多次调用“services.AddIdentity”。在我的项目中每次使用该插件一次。

              无论如何解决办法是更换:

              services.AddIdentity<IdentityUser, IdentityRole>((options) => { ... })....
              

              与:

              services.AddIdentityCore<IdentityUser>((options) => { ... }).AddRoles<IdentityRole>()....
              

              后者显然捕获了异常。

              【讨论】:

              • 忘了说:我在 Debian 10 上使用 .NET 5。
              【解决方案8】:

              如果从“Startup.cs”中删除“addDefaultIdentity”对您不起作用。

              services.AddDefaultIdentity&lt;ApplicationUser&gt; 替换为AddIdentity&lt;ApplicationUser, IdentityRole&gt;

              Code snippet

              /*
              services.AddIdentity<ApplicationUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
                  .AddEntityFrameworkStores<ApplicationDbContext>();
              */
              
              services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
                  .AddEntityFrameworkStores<ApplicationDbContext>();
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2023-04-07
                • 2019-09-20
                • 2019-01-17
                • 2018-06-27
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多