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

时间:2019-06-03 18:58:42

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

我想为用户添加自己的自定义数据,因此我在这里关注本教程: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();
        }

我觉得我不应该引发异常,但是...关于修复的任何建议吗?

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

2 个答案:

答案 0 :(得分:0)

尝试将您的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();

答案 1 :(得分:0)

这个 ASP 身份核心 InvalidOperationException 主要被抛出,当 Startup.cs 或使用 ASP 身份核心的项目中的任何类中存在对函数的重复调用时,ASP 身份在启动时需要什么班级:

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

}