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

.NET 6项目登录时遇MissingMethodException错误求解决

解决方案

核心原因

该错误源于NuGet包版本与.NET 6 runtime不兼容,尤其是加密、身份验证相关包(如Microsoft.AspNetCore.Cryptography.Internal)的方法签名和框架内置实现不匹配,导致找不到指定方法。布局加载失败多为错误引发的连锁问题,解决MissingMethodException后通常可恢复。

具体修复步骤

  • 清理重建项目并还原NuGet包
    1. 删除项目根目录下的bin和obj文件夹
    2. 在终端执行命令:
      dotnet clean
      dotnet restore
      
  • 统一ASP.NET Core相关包版本
    打开项目.csproj文件,确保所有ASP.NET Core类NuGet包(如Microsoft.AspNetCore.Identity.EntityFrameworkCore、Microsoft.AspNetCore.DataProtection)的版本与.NET 6 SDK版本一致(通常为6.0.x),示例配置:
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.25" />
    <PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="6.0.25" />
    
  • 确认目标框架配置
    检查.csproj中的目标框架是否为net6.0,避免版本指定错误:
    <TargetFramework>net6.0</TargetFramework>
    
  • 排查登录代码与布局引用
    • 检查AccountController中身份验证服务注入与调用是否正确,示例正确逻辑:
      private readonly SignInManager<IdentityUser> _signInManager;
      
      public AccountController(SignInManager<IdentityUser> signInManager)
      {
          _signInManager = signInManager;
      }
      
      [HttpPost]
      [AllowAnonymous]
      [ValidateAntiForgeryToken]
      public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
      {
          returnUrl ??= Url.Content("~/");
          if (ModelState.IsValid)
          {
              var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
              if (result.Succeeded) return LocalRedirect(returnUrl);
              if (result.IsLockedOut) return RedirectToAction(nameof(Lockout));
              ModelState.AddModelError(string.Empty, "Invalid login attempt.");
          }
          return View(model);
      }
      
    • 检查Login.cshtml的布局引用是否正确,确保顶部有:
      @{
          Layout = "_Layout";
      }
      
  • 清除本地NuGet缓存(若上述步骤无效)
    执行命令清除缓存后重新还原:
    dotnet nuget locals all --clear
    dotnet restore
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 17:07:06