You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Standard 2.0中ASP.NET Core Identity引发MissingMethodException的替代方案问询

解决ASP.NET Core Identity在.NET Standard 2.0 + .NET Framework 4.7.2 WebForms中的MissingMethodException问题

问题根源

你遇到的System.MissingMethodException本质是版本不兼容:

  • 你使用的Microsoft.AspNetCore.Identity 2.2.0依赖的是Entity Framework Core 2.x版本,但类库中引用的EF Core是3.1.1
  • EF Core 3.x对HasIndex方法的签名做了修改,Identity 2.2.x的代码仍在调用旧版本的方法,导致运行时找不到对应实现

解决方案1:统一依赖版本(最稳妥)

将类库中所有相关依赖的版本统一到与Microsoft.AspNetCore.Identity 2.2.0兼容的EF Core 2.x系列,具体步骤:

  1. 修改.NET Standard类库的项目文件,更新PackageReference:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.8" />
    <PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.2.0" />
  </ItemGroup>
</Project>

注:EF Core 2.2.8是该分支的最后稳定版本,与Identity 2.2.0完全兼容

  1. 优化UserManager的创建代码(避免传入过多null,减少潜在问题):
public static void doStuff()
{
    var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
    optionsBuilder.UseSqlServer("MyConnectionString");
    var dbContext = new ApplicationDbContext(optionsBuilder.Options);
    
    var userStore = new UserStore<ApplicationUser, MyRole, ApplicationDbContext, Guid>(dbContext);
    
    // 提供必要的Identity选项配置
    var identityOptions = new IdentityOptions();
    var passwordValidator = new PasswordValidator<ApplicationUser>();
    var userValidators = new List<IUserValidator<ApplicationUser>> { new UserValidator<ApplicationUser>() };
    
    // 使用NullLogger适配(也可以替换为WebForms项目的日志实现)
    var logger = new NullLogger<UserManager<ApplicationUser>>();
    
    var userManager = new UserManager<ApplicationUser>(
        userStore, 
        new OptionsWrapper<IdentityOptions>(identityOptions), 
        new PasswordHasher<ApplicationUser>(),
        userValidators,
        new List<IPasswordValidator<ApplicationUser>> { passwordValidator },
        new UpperInvariantLookupNormalizer(),
        new IdentityErrorDescriber(),
        null, // 依赖注入容器,WebForms中可传null
        logger
    );
    
    var users = userManager.Users; // 现在应该可以正常访问
}

解决方案2:升级Identity到3.x(如果兼容WebForms)

你提到担心Microsoft.AspNetCore.Identity 3.x与.NET Standard 2.0不兼容,但实际上3.1.x版本的Identity是支持.NET Standard 2.0的。如果WebForms项目可以引入必要的.NET Core依赖包(如Microsoft.Extensions.DependencyInjection、Microsoft.Extensions.Logging等),可以尝试升级所有依赖到3.1.x版本,保持版本一致:

修改类库项目文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Identity" Version="3.1.32" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.32" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.32" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.32" />
    <PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="3.1.32" />
  </ItemGroup>
</Project>

注:升级后需要确保WebForms项目也引用了这些包的对应版本,避免运行时依赖冲突


关键注意事项

  • 始终保持ASP.NET Core Identity、Identity.EntityFrameworkCore、EF Core三者的版本一致,这是避免此类版本冲突的核心原则
  • WebForms项目中使用ASP.NET Core Identity时,需要确保所有依赖包的版本在类库和WebForms项目中保持同步
  • 如果数据库已经迁移,升级EF Core版本后可能需要重新生成迁移脚本(但如果是同分支内的版本升级,通常不需要修改数据库结构)

内容的提问来源于stack exchange,提问作者user3675331

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 12:37:33