System.InvalidOperationException:方案已经存在:Identity.Application [英] System.InvalidOperationException: Scheme already exists: Identity.Application

查看:81
本文介绍了System.InvalidOperationException:方案已经存在:Identity.Application的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为用户添加自己的自定义数据,因此我在这里关注该教程: https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/add-user-data?view = aspnetcore-2.2& tabs = visual-studio

I wanted to add my own custom data for users so I was following the tutorial here: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/add-user-data?view=aspnetcore-2.2&tabs=visual-studio

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

I already had an existing application so I could not follow that tutorial line by line(my existing application has a database of users already). I didn't get very far in it when I hit the above error. I used the scaffolder to try to add

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

System.InvalidOperationException: Scheme already exists: Identity.Application

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

I've gone to a couple of different stack overflow and git pages suchas the following to no avail

https://github.com/aspnet/AspNetCore.Docs/issues/8223(我认为最相关) https://github.com/aspnet/Security/issues/1412 AddIdentity()失败"InvalidOperationException:方案已经存在:Identity.Application".

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

it seems like a lot of other people add problems with calling the identity twice but I'm only calling it once in my code. I've also tried commenting out the line entirely in the startup and then it says there is none defined and gets mad at me. I've also tried switch form the default as shown below.

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

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

I feel like I shouldn't be getting the exception thrown and yet.... any advice on a fix?

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

edit: relevant steps i took until i got this error. Create project conent to use invidual user accounts in process creation.override with scaffolder, and create a secondary user model that you can override. migrate and update database run.

推荐答案

尝试将您的 IdentityUser 类重命名为AspNetIdentity类中的唯一类.然后确保您要从IdentityUser继承

Try renaming your IdentityUser class to something unique from AspNetIdentity classes. Then make sure you are inheriting from 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();

这篇关于System.InvalidOperationException:方案已经存在:Identity.Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆