asp.net-identity - AddIdentity() 失败 "InvalidOperationException: Scheme already exists: Identity.Application"

标签 asp.net-identity asp.net-core-2.0

我正在尝试将 facebook 登录添加到我的 .NET Core 2.1 网站

我正在关注this ,指南,更具体,this (for facebook login)

将以下行添加到startup.cs后,在ConfigureServices-method内

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
    ...
}

当我运行应用程序时,我收到错误消息。如果没有这些行,它就可以“正常”工作。用户可以登录 Facebook 并批准我的申请,我会收到电子邮件等等。但我猜用户信息不会添加到我的数据库中

InvalidOperationException: Scheme already exists: Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(string name, Action configureBuilder)...

代码越过添加的行,不久后(启动期间)就会出现错误。我在我的项目(这只是一个裸露的 .NET Core 2.1 Web 应用程序,来自模板)中进行了搜索,但我看不到“AddIdentity”的任何其他用法。

我确实找到了一个“AddDEfaultIdentity()”,将该行注释掉。但后来我得到了

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered. Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)

最佳答案

我也有类似的问题。这对于使用 .Net Core 3.0 的人可能更有帮助。经过一番挖掘后,我发现一旦你使用脚手架创建了一个“身份”区域。在 Identity 文件夹内创建一个名为“IdentityHostingStartup.cs”的文件。

IdentityHostingStartup Location

在该类中,将创建“AddDefaultIdentity”的另一个实例以及一些其他服务。

IdentityHostingStartup File View

如果您从“Startup.cs”中删除“addDefaultIdentity”,您的应用程序应该会启动。另外,如果您收到空连接字符串错误。更新 IdentityHostintgStartup.cs 内的连接字符串

注意:删除任一 addDefaultIdentities 即可。您不能在两个位置都拥有它。

关于asp.net-identity - AddIdentity() 失败 "InvalidOperationException: Scheme already exists: Identity.Application",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51161729/

相关文章:

asp.net-mvc - ASP.NET MVC Identity - 如何更改 SMS 中生成的 token 的格式

postgresql - 系统参数异常 : 'Keyword not supported: ' host'. '

authentication - Asp.NET Core 2 身份验证。 SignInManager.PasswordSignInAsync() 成功,但 User.Identity.IsAuthenticated 为 false

c# - Asp.Net Core 2.0 ArgumentNullException : Value cannot be null. 参数名称:connectionString

entity-framework - 使用通用 dbContext 获取通用存储库(多个 dbContext)

asp.net-mvc - 如何从 Owin Middleware 正确登录用户

c# - ASP.NET Identity - 每种类型多个对象集

asp.net - 电子邮件 token 在 15 分钟后过期 - Asp Identity 2.0 API

c# - .Net Core 自定义身份验证使用 Identity Server 4 的 API key

javascript - 使用 identityserver4 和 aspnet identity 时如何安全地实现空闲超时?