问题
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: Scheme already exists: Identity.Application
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 (most relevant I think) https://github.com/aspnet/Security/issues/1412 AddIdentity() fails "InvalidOperationException: Scheme already exists: 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.
回答1:
Try renaming your IdentityUser
class to something unique from AspNetIdentity classes. Then make sure you are inheriting from IdentityUser
For example here is a class
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsDisabled { get; set; }
}
And this is the startup
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<IdentityContext>()
.AddDefaultTokenProviders();
来源:https://stackoverflow.com/questions/56433112/system-invalidoperationexception-scheme-already-exists-identity-application