无效操作异常:无法解析类型"Microsoft. AspNetCore. Identity. UserManager"的服务

t98cgbkg  于 2023-02-01  发布在  .NET
关注(0)|答案(1)|浏览(291)
    • 错误:**

处理请求时发生未处理的异常。InvalidOperationException:尝试激活"TestApp_03_RazorPage_Identity.Areas.Identity.Pages.Account.RegisterModel"时,无法解析类型"Microsoft. AspNetCore. Identity.用户管理器" 1 [Microsoft. AspNetCore. Identity.标识用户]"的服务。Microsoft.扩展.依赖注入.激活器实用程序. GetService(IServiceProvider sp,类型类型,所需类型,bool是默认参数必需的)

    • 详细信息**:我正在按照微软的例子,当我试图设置一个ApplicationUser类时,它仍然不起作用。我看到类似的帖子,没有什么对我起作用
    • 程序. cs**
var builder = WebApplication.CreateBuilder(args);
var connectionString = 
builder.Configuration.GetConnectionString("MyConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(connectionString));

builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

builder.Services.AddRazorPages();
    • 应用程序用户. cs**
public class ApplicationUser : IdentityUser
{
    public string FullName { get; set; }
}
    • 应用程序数据库上下文. cs**
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
 {
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}
  • _部分登录. cshtml*
@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
    • 寄存器型号**
public class RegisterModel : PageModel
{
    private readonly SignInManager<ApplicationUser> _signInManager;
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly IUserStore<IdentityUser> _userStore;
    private readonly IUserEmailStore<IdentityUser> _emailStore;
    private readonly ILogger<RegisterModel> _logger;
    private readonly IEmailSender _emailSender;
rjee0c15

rjee0c151#

如果扩展IdentityUser,则需要使用ApplicationUser

private readonly IUserStore<ApplicationUser> _userStore;
private readonly IUserEmailStore<ApplicationUser> _emailStore;

而不是:

private readonly IUserStore<IdentityUser> _userStore;
private readonly IUserEmailStore<IdentityUser> _emailStore;

相关问题