无法在身份脚手架中选择自定义用户类别

问题描述 投票:0回答:1

我正在尝试在 ASP.NET Core 应用程序中搭建身份页面(登录和注册)。但是,当我选择页面和 DbContext 类时,我无法选择自定义用户类

ApplicationUser
,因为“用户类”字段已禁用。

这是我的 DbContext 类定义:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>

我的自定义用户和角色:

public class ApplicationUser : IdentityUser
public class ApplicationRole : IdentityRole

我在上下文中引用了我的

ApplicationUser
ApplicationRole
类。此外,我尝试回滚 Microsoft.AspNetCore.Identity.UI 包的版本,但问题仍然存在。

什么可能导致用户类别字段被禁用,以及如何启用它来选择我的自定义用户类别?

我尝试手动将这些文件中的

IdentityUser
替换为
ApplicationUser
:Register.cshtml.cs 和 Login.cshtml.cs,但它不起作用,我收到此错误:

InvalidOperationException:尝试激活“MyApp.Web.Areas.Identity.Pages.Account.RegisterModel”时无法解析类型“Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]”的服务。

c# asp.net-core asp.net-core-identity asp.net-mvc-scaffolding
1个回答
0
投票

解决方案

这个问题的解决方案是遍历每个脚手架页面,并将

IdentityUser
替换为
ApplicationUser

示例

// NOT Working
public class LoginModel: PageModel {
  private readonly SignInManager <IdentityUser> _signInManager;
  private readonly ILogger <LoginModel> _logger;

  public LoginModel(SignInManager <IdentityUser> signInManager, ILogger <LoginModel> logger) {
    _signInManager = signInManager;
    _logger = logger;
  }
}

// Working
public class LoginModel: PageModel {
  private readonly SignInManager <ApplicationUser> _signInManager;
  private readonly ILogger <LoginModel> _logger;

  public LoginModel(SignInManager <ApplicationUser> signInManager, ILogger <LoginModel> logger) {
    _signInManager = signInManager;
    _logger = logger;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.