![](/img/trans.png)
[英]System.InvalidOperationException: Scheme already exists: Identity.Application: After Identity Scaffolding
[英]An error occurred while starting the application. InvalidOperationException: Scheme already exists: Identity.Application
启动应用程序时发生错误。
InvalidOperationException:Scheme 已经存在:Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(string name, Action configureBuilder)
我正在 VS 2017 中使用 Angular 模板处理 ASP.NET Core WEB API。我在 Statrtup.cs 类的 ConfigureServices() 方法中有以下代码
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AuthDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("AuthDbContextConnection"));
});
services.AddDbContext<AppNgDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("AppNgDbContextConnection"));
});
services.AddTransient<SecurityService>();
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AuthDbContext>()
.AddDefaultTokenProviders();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
// 1. Load the JST Secret Key to Verify and Validate Token
// read key from appsettings.json
var secretKey = Convert.FromBase64String(Configuration["JWTAppSettings:SecretKey"]);
// 2. Defining the Mechanism for Validating Received Token from Client
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(secretKey)
};
});
services.AddScoped<IRepository<Orders, int>, OrdersRepository>();
services.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
当我运行应用程序时,它应该加载以便我可以访问 WEB API 但不幸的是它产生以下错误
启动应用程序时发生错误。
InvalidOperationException:Scheme 已经存在:Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(string name, Action configureBuilder)
InvalidOperationException:Scheme 已经存在:Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(string name, Action configureBuilder) Microsoft.AspNetCore.Authentication.AuthenticationBuilder+<>c__DisplayClass4_0.b__0(AuthenticationOptions o) Microsoft.Extensions.Options.ConfigureNamedOptions.Configure (字符串名称,TOptions 选项) Microsoft.Extensions.Options.OptionsFactory.Create(字符串名称) Microsoft.Extensions.Options.OptionsManager+<>c__DisplayClass5_0.b__0() System.Lazy.ViaFactory(LazyThreadSafetyMode 模式) System.Lazy.ExecutionAndPublication(LazyHelper) executionAndPublication, bool useDefaultConstructor) System.Lazy.CreateValue() Microsoft.Extensions.Options.OptionsCache.GetOrAdd(string name, Func createOptions) Microsoft.Extensions.Options.OptionsManager.Get(string name) Microsoft.Extensions.Options.OptionsManager.get_Value () Microsoft.AspNetCore.Authentication.AuthenticationSchemeProvider..ctor(IOptions options, IDictionary scheme) Microsoft.AspNetCore.Authentication.AuthenticationSchemeProvider..ctor(IOptions 选项) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope) Microsoft.Extensions.DependencyInjection.ServiceLookuptor.CallSiteSiteSiteSiteSiteCalls Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite.ResolverSingletonSingletonCallSite.ResolverSingletonSingleton(SingletonCallSite.ResolverSingletonSite.CallSite.Services. , TArgument 参数) Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine+<>c__DisplayClass1_0.b__0(ServiceProviderEngineScope scope) Microsoft。 Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType) Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) Microsoft.Extensions.Internal ActivatorUtilities+ConstructorMatcher.CreateInstance(IServiceProvider provider) Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, object[] parameters) Microsoft.AspNetCore.Builder.UseMiddlewareExtensions+<>c__DisplayClass4_0.b__0(RequestDelegate next) Microsoft.AspNetCore。 Builder.Internal.ApplicationBuilder.Build() Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
我有一个类似的问题。 这可能对使用 .Net Core 3.0 的人更有帮助。 在挖掘之后,我发现一旦你使用脚手架创建了一个“身份”区域。 在 Identity 文件夹中创建了一个名为“IdentityHostingStartup.cs”的文件。
在该类中,创建了“AddDefaultIdentity”的另一个实例以及一些其他服务。
如果您从“Startup.cs”中删除“addDefaultIdentity”,您的应用程序应该会启动。 此外,如果您收到空连接字符串错误。 更新 IdentityHostintgStartup.cs 中的连接字符串
注意:删除任一 addDefaultIdentities 都可以。 你不能在两个地方都拥有它。
希望这可以帮助。
经过一些尝试后,我发现以下几行对我有效
services.AddIdentityCore<IdentityUser>().AddRoles<IdentityRole>()
.AddEntityFrameworkStores<AuthDbContext>()
.AddDefaultTokenProviders();
我添加了这个而不是以下代码
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores()。AddDefaultTokenProviders();
这对我来说只需要使用完整的5-6小时。
谢谢Mahesh Sabnis
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.