【发布时间】: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<IdentityUser, IdentityRole>()中的类IdentityUser还是属于 AspNetIdentity 的IdentityUser类?如果是您需要特别声明,否则 AspNetIdentity 会认为您正在尝试将 IdentityUser 声明为它自己的 IdentityUser 类,该类抛出异常。 -
另一个场景:您使用 Identity Scaffold 在部分构建的身份验证设置上自动生成。在这种情况下,您会在 Areas\Identity....中看到创建用户的宏。
标签: asp.net asp.net-core asp.net-authentication