启动应用程序时发生错误。
InvalidOperationException:方案已经存在:Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(字符串名称,操作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:方案已经存在:Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(字符串名称,操作configureBuilder)
InvalidOperationException:方案已经存在:Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(字符串名称,操作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(LazyHelperexecutionAndPublication,bool useDefaultConstructor) System.Lazy.CreateValue() Microsoft.Extensions.Options.OptionsCache.GetOrAdd(字符串名称,Func createOptions) Microsoft.Extensions.Options.OptionsManager.Get(字符串名称) Microsoft.Extensions.Options.OptionsManager.get_Value() Microsoft.AspNetCore.Authentication.AuthenticationSchemeProvider..ctor(IOptions选项,IDictionary方案) Microsoft.AspNetCore.Authentication.AuthenticationSchemeProvider..ctor(IOptions选项) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite构造函数CallSite,ServiceProviderEngineScope范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(IServiceCallSite callSite,TArgument参数) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite,ServiceProviderEngineScope范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite,ServiceProviderEngineScope范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(IServiceCallSite callSite,TArgument参数) Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine + <> c__DisplayClass1_0.b__0(ServiceProviderEngineScope范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(类型serviceType,ServiceProviderEngineScope serviceProviderEngineScope) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType) Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型serviceType) Microsoft.Extensions.Internal.ActivatorUtilities + ConstructorMatcher.CreateInstance(IServiceProvider提供程序) Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider提供程序,类型instanceType,对象[]参数) Microsoft.AspNetCore.Builder.UseMiddlewareExtensions + <> c__DisplayClass4_0.b__0(RequestDelegate接下来) Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build() Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
答案 0 :(得分:0)
尝试一下后,我发现以下几行对我有用
services.AddIdentityCore<IdentityUser>().AddRoles<IdentityRole>()
.AddEntityFrameworkStores<AuthDbContext>()
.AddDefaultTokenProviders();
我添加了此代码,而不是以下代码
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores() .AddDefaultTokenProviders();
这只花了5到6个小时就对我有用了。
谢谢 马赫什·萨布尼斯(Mahesh Sabnis)
答案 1 :(得分:0)
我有一个类似的问题。这对使用.Net Core 3.0的人可能更有用。深入研究后,我发现一旦您使用脚手架创建了“身份”区域。在Identity文件夹中创建了一个名为“ IdentityHostingStartup.cs”的文件。
在类中,将创建“ AddDefaultIdentity”的另一个实例以及其他一些服务。
如果从“ Startup.cs”中删除“ addDefaultIdentity”,则您的应用应启动。另外,如果收到空连接字符串错误。更新IdentityHostintgStartup.cs内部的连接字符串
注意:删除两个addDefaultIdentities都可以。您只是不能在两个地方都拥有它。
希望这会有所帮助。