【问题标题】:System.InvalidOperationException: Scheme already exists: Identity.ApplicationSystem.InvalidOperationException:方案已存在:Identity.Application
【发布时间】:2023-04-07 06:08:01
【问题描述】:

我想为用户添加我自己的自定义数据,所以我按照这里的教程进行操作:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/add-user-data?view=aspnetcore-2.2&tabs=visual-studio

我已经有一个现有的应用程序,所以我无法逐行遵循该教程(我现有的应用程序已经有一个用户数据库)。当我遇到上述错误时,我并没有走得太远。我用脚手架尝试添加

System.InvalidOperationException:方案已存在:Identity.Application

我已经去了几个不同的堆栈溢出和 git 页面,如以下无济于事

https://github.com/aspnet/AspNetCore.Docs/issues/8223(我认为最相关) https://github.com/aspnet/Security/issues/1412 AddIdentity() fails "InvalidOperationException: Scheme already exists: Identity.Application"

似乎很多其他人都添加了两次调用身份的问题,但我只在我的代码中调用了一次。我还尝试在启动时完全注释掉该行,然后它说没有定义并且生我的气。我也尝试过切换默认值,如下所示。

    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<IdentityUser, IdentityRole>()
       // services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<WebApp1.Models.WebApp1Context>()
            .AddDefaultTokenProviders();


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAuthentication();

            app.UseMvc();
        }

我觉得我不应该抛出异常,但......有什么关于修复的建议吗?

编辑:我在收到此错误之前采取的相关步骤。创建项目内容以在流程创建中使用个人用户帐户。使用脚手架覆盖,并创建可以覆盖的辅助用户模型。迁移和更新数据库运行。

【问题讨论】:

  • services.AddIdentity&lt;IdentityUser, IdentityRole&gt;() 中的类IdentityUser 还是属于 AspNetIdentity 的 IdentityUser 类?如果是您需要特别声明,否则 AspNetIdentity 会认为您正在尝试将 IdentityUser 声明为它自己的 IdentityUser 类,该类抛出异常。
  • 另一个场景:您使用 Identity Scaffold 在部分构建的身份验证设置上自动生成。在这种情况下,您会在 Areas\Identity....中看到创建用户的宏。

标签: asp.net asp.net-core asp.net-authentication


【解决方案1】:

尝试将您的 IdentityUser 类重命名为 AspNetIdentity 类的独特名称。然后确保您是从 IdentityUser 继承的

例如这里是一个类

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool IsDisabled { get; set; }
}

这就是启动

services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<IdentityContext>()
            .AddDefaultTokenProviders();

【讨论】:

【解决方案2】:

这个 ASP Identity Core InvalidOperationException 主要被抛出,当 Startup.cs 或项目中使用 ASP Identity Core 的任何类中存在对函数的重复调用时,ASP Identity 在 Startup 类中工作需要什么:

public void ConfigureServices(IServiceCollection services)
{
   _ = services.AddDbContext<ApplicationDbContext>(options =>                
  options.UseSqlServer(Configuration.GetConnectionString("AmpCoreDb")));

   _ = services.AddDefaultIdentity<IdentityUser>(options =>
  options.SignIn.RequireConfirmedAccount = true)
         .AddEntityFrameworkStores<ApplicationDbContext>();

  _ = services.AddScoped<AuthenticationStateProvider, 
  RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();

  _ = services.AddDatabaseDeveloperPageExceptionFilter();

}

【讨论】:

    【解决方案3】:

    标记的答案不是正确答案。我遇到了同样的问题,原因是当添加所有标识脚手架时(通过 dotnet aspnet-codegenerator as Visual Studio 崩溃),它在区域/标识下创建了一个 IdentityHostingStartup 类。此类复制了 startup.cs 中的身份设置。所以删除这个类解决了这个问题。

    【讨论】:

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