【问题标题】:net core 2.2 local customized user account System.InvalidOperationExceptionnet core 2.2 本地自定义用户帐号 System.InvalidOperationException
【发布时间】:2019-01-03 20:42:44
【问题描述】:

我使用本地用户帐户在 net core 2.2 中创建了项目,并使用脚手架添加了所有身份文件。我在模型文件夹中创建了两个文件 ApplicationUser.csApplicationRole.cs。然后我修改了startup.csapplicationdbcontext 为用户和角色使用新属性。现在我必须在Areas/Identity 的所有类中将所有关系从IdentityUser 更改为ApplicationUser。我是否遗漏了什么或者这是预期的行为?

在将关系从 IdentityUser 更改为 ApplicationUser 之前出现错误。

在尝试激活“Areas.Identity.Pages.Account.LogoutModel”时,无法解析“Microsoft.AspNetCore.Identity.SignInManager`1[Microsoft.AspNetCore.Identity.IdentityUser]”类型的服务。 私有只读 SignInManager _signInManager;

private readonly SignInManager<ApplicationUser> _signInManager;

public class ApplicationRole : IdentityRole
{
    public string Description { get; set; }
    public string DisplayName { get; set; }
}

 public class ApplicationUser : IdentityUser
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Fullname { get; set; }
 }

startup.cs

services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

我想确保我没有遗漏或犯错。

【问题讨论】:

  • 在您的控制器或视图中 UserManager 更改为 UserManager
  • 我知道而且我做到了。它有效,但我必须在身份区域中的每个文件/类中进行更改。我正在寻找复杂的解决方案。

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


【解决方案1】:

根据您显示的代码,对我来说突出的主要问题是您从未将任何内容分配给_signInManager

class Foo // your class
{
    private readonly SignInManager<ApplicationUser> _signInManager;

    public Foo() // your constructor
    {
        _signInManager = ; //<-- this
    }
}

此时,表明它被定义为null

【讨论】:

  • 我知道。我必须在身份区域中的每个文件/类中进行更改。我正在寻找复杂的解决方案。
猜你喜欢
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 2020-04-17
  • 2019-03-31
  • 1970-01-01
  • 2023-02-01
  • 2018-01-23
相关资源
最近更新 更多