【问题标题】:IServiceCollection does not contain a definition for AddDefaultIdentityIServiceCollection 不包含 AddDefaultIdentity 的定义
【发布时间】:2020-08-20 11:33:10
【问题描述】:

知道为什么会出现此错误吗?错误消息 --> “IServiceCollection 不包含 AddDefaultIdentity 的定义”

public class Program
{
    public async static void Main(string[] args)
    {
        await Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder => {
           webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
           webBuilder.UseKestrel();
           webBuilder.UseAzureAppServices();
           webBuilder.UseStartup<Startup>();
       })
      .Build()
      .RunAsync();
   }
}

public class Startup
{
    public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment)
    {
        Configuration = configuration;
        HostEnvironment = hostEnvironment;
    }

    public IConfiguration Configuration { get; }
    protected IApplicationBuilder ApplicationBuilder { get; private set; }
    public IHostEnvironment HostEnvironment { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        //           services.AddRazorPages();

        services.AddDefaultIdentity<ApplicationUser>()  // "ApplicationUser" is named incorrectly, it should be "IdentityUser" instead, as per Microsoft documentation.
            .AddRoles<IdentityRole<Guid>>()
            .AddEntityFrameworkStores<ApplicationContext, Guid>()  // FYI - AddEntityFrameworkStores() deal with role that derives from IdentityRole, as per documentation.
            //.AddDefaultUI()
            .AddDefaultTokenProviders();

        // [ Old version #1 - replacement ]
        services.ConfigureApplicationCookie(options =>
        {
            options.LoginPath = new PathString("/Home/Index");
            options.SlidingExpiration = true;
            options.ExpireTimeSpan = TimeSpan.FromMinutes(this.Configuration.GetValue<int?>("Authentication:SlidingExpirationTime").Value);
            options.AccessDeniedPath = new PathString("/Home/AccessDenied");
        });

        // [ Old version #2 - replacement ]
        services.Configure<IdentityOptions>(options =>
        {
            options.Password.RequireUppercase = false;
            options.Password.RequireLowercase = false;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireDigit = false;
            options.Password.RequiredLength = 7;
        });

        services.AddMvc();
        services.AddSession();

        //services.Configure<AuthorizationOptions>(options =>
        //{
        //});
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
           app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });

        // Config Exception.
        if (env.IsDevelopment())
            app.UseDeveloperExceptionPage();
        else
            app.UseExceptionHandler("/Home/ErrorPage.html");
        
        app.UseStaticFiles(); // Note, we are not authenticating for static files if this is before them
        app.UseSession();
        app.UseAuthentication();

        // MVC.
        // app.UseMvc(routes => routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"));
    }
}

public class ApplicationUser : IdentityUser<Guid>, IUser
{
}

public interface IUser
{
}

public class ApplicationContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
{
    public ApplicationContext(DbContextOptions<ApplicationContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="3.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.DataProtection.AzureStorage" Version="1.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Xml" Version="1.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.Session" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.3" />
    <PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="3.1.3" />
    <PackgaeReference Include="Microsoft.Extensions.Hosting" Version="3.1.3" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="1.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.AzureAppServicesIntegration" Version="1.0.2" />
  </ItemGroup>
</Project>

-- 已编辑 - 下面有新的更新。 -------------------------------------------------- -----

好的,通过添加“Microsoft.AspNetCore.Identity.UI”NuGet 包来提供帮助。现在我遇到了另一个错误。 :-/ 我无法理解这个。

services.AddDefaultIdentity<ApplicationUser>() 
.AddRoles<IdentityRole<Guid>>()
.AddEntityFrameworkStores<ApplicationContext, Guid>() 
.AddDefaultTokenProviders();

第一行的错误现在消失了。但是现在这里的第 3 行出现了新错误,“AddEntityFrameworkStore()”。错误消息是-->“IdentityBuilder”不包含“AddEntityFrameworkStores”的定义,并且找不到接受“IdentityBuilder”类型的第一个参数的可访问扩展方法“AddEntityFrameworkStores”(您是否缺少 using 指令或程序集引用?)。

甚至不确定这个“AddEntityFrameworkStores”来自什么 NuGet 包以及从版本 1 到 3.1 的变化。

【问题讨论】:

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


    【解决方案1】:

    我不知道这是什么时候加入的,但在 AspNetCore 5.0 中:services.AddIdentityCore&lt;ApplicationUser&gt;() 有效。仅包含 TUser 的类型要求。

    【讨论】:

      【解决方案2】:

      您需要添加对Microsoft.AspNetCore.Identity.UI nuget package 的引用才能使用AddDefaultIdentity。但是,如果您不想迁移到 Identity Razor 类库,我认为您仍然可以在核心 3.1 中使用 .AddIdentity&lt;ApplicationUser, IdentityRole&gt;()。如果您确实想迁移到 RCL,2.0 到 2.1 的迁移文档可能是一个不错的起点: https://docs.microsoft.com/en-us/aspnet/core/migration/20_21?view=aspnetcore-3.1#changes-to-authentication-code

      ---已编辑---

      我已经将一些网站从 1.1 迁移到 3.1,我发现的最简单的方法是:

      1. 将整个解决方案移至备份文件夹(确保将源代码控制文件保留在原处)。
      2. 在您的原始位置创建一个与目标 3.1 完全相同的名称的新应用程序。我在 VS 2019 中使用“Web 应用程序(模型-视图-控制器)”模板,并将身份验证更改为“个人用户帐户”。
      3. 将其提交到源代码管理,以便您查看所做的更改。
      4. 将所有页面、视图控制器和其他代码复制到新应用程序中。
      5. 重新添加所有缺失的 nuget 包。
      6. 您可能需要对复制的代码进行一些更改,但您可以使用源代码管理和迁移文档中的更改作为参考。

      它仍然需要很多时间才能使其正常工作,但否则您将需要阅读每个迁移文档,从 1.x 到 2.0 的文档一直到 3.0 到 3.1 的文档。

      【讨论】:

      • 当我添加 NuGet 包时,我收到 .AddEntityFrameworkStore&lt;ApplicationContext, Guid&gt;() 错误,提示“IdentityBuilder 不包含“AddEntityFrameworkStores 的定义,并且没有可访问的扩展方法 AddEntityFrameworkStores 可以找到接受 IdentityBuilder 类型的第一个参数(您是否缺少使用目录或程序集引用?)“。所以也不知道如何修复那个。这个方法就在 AddDefaultIdentity() 错误之后,距离它只有 2 行。
      • 在我的应用程序中,我有这样的:.AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()。我还用另一个迁移建议更新了我的答案。
      • 我之前在 VS 2019 中添加了新项目,一切都是空的。因此,我一直在寻找一个示例解决方案/项目,以便从中下载几乎所有内容。厨房水槽样品会让我更快到达那里。
      • 我在VS 2019中使用“Web应用程序(模型-视图-控制器)”模板,并将认证改为“个人用户帐户”
      • 太好了!直到现在,我前几次都看不到身份验证选项,因为“更改”链接很难找到。好的,现在我现在更好地解决了这个问题。如果我使用 .AddEntityFrameworkStores&lt;ApplicationContext&gt;(),它可以工作,但如果我使用 Guid 部分,.AddEntityFrameworkStores&lt;ApplicationContext, Guid&gt;() 它不起作用。好的,需要在不破坏旧代码的情况下弄清楚为什么会这样。 :-/
      猜你喜欢
      • 1970-01-01
      • 2019-10-19
      • 1970-01-01
      • 2019-06-09
      • 2022-01-03
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 2019-04-14
      相关资源
      最近更新 更多