为什么 UserManager.CreateIdentityAsync() 正在寻找 IdentityRole 以及如何修复?

2023-11-22

我在用着身份2.0 with MVC5 代码优先我已经延长了IdentityUser and IdentityRole像这样:

public class ApplicationUser : IdentityUser
{
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }
}

public class ApplicationRole : IdentityRole
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }

    [Required]
    public string Description { get; set; }
}

public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
    public MyAppDb()
        : base("MyAppDb")
    {
    }
}

Notice MyAppDb继承自IdentityDbContext但我通过了ApplicationRole代替IdentityRole. My ApplicationRole继承自IdentityRole所以这应该不是问题。

BUT...

My AccountController抛出这个错误:

The entity type IdentityRole is not part of the model for the current context. 

At the UserManager.CreateIdentityAsync(...)在这段代码中:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

The user正在这里创建:

        var user = await UserManager.FindAsync(model.UserName, model.Password);
        if (user != null)
        {
            await SignInAsync(user, model.RememberMe);
            return RedirectToLocal(returnUrl);
        }

什么是“重大故障”? ;)

UPDATE:我相当确定这个问题与UserStore here:

    public class AccountController : Controller
    {
        public AccountController ()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyAppDb())))
        {
        }

        public AccountController (UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
            UserManager.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);
            UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager) { AllowOnlyAlphanumericUserNames = false };
        }

        public UserManager<ApplicationUser> UserManager { get; private set; }
...

我如何获得UserStore了解ApplicationRole当我只能传递一个参数时?


我通过创建一个解决了这个问题ApplicationUserStore and ApplicationUserManager:

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

}

public class ApplicationUserLogin : IdentityUserLogin<string>
{
}

public class ApplicationUserClaim : IdentityUserClaim<string>
{
}

public class ApplicationUserRole : IdentityUserRole<string>
{
}

public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }
}


public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public static MyAppDb Create()
    {
        return new MyAppDb();
    }

    public MyAppDb()
        : base("MyAppDb")
    {
    }
}


public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUserStore(MyAppDb context)
        : base(context)
    {
    }
}

public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
    public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options)
    {
        var manager = new ApplicationUserManager(new ApplicationUserStore(new MyAppDb()));
        // Configure the application user manager
        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };
        manager.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);

        return manager;
    }
}

public class ApplicationRoleStore : RoleStore<ApplicationRole, string, ApplicationUserRole>
{
    public ApplicationRoleStore(MyAppDb context)
        : base(context)
    {
    }
}

public class ApplicationRoleManager : RoleManager<ApplicationRole, string>
{
    public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store)
        : base(store)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options)
    {
        var manager = new ApplicationRoleManager(new ApplicationRoleStore(new MyAppDb()));

        return manager;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 UserManager.CreateIdentityAsync() 正在寻找 IdentityRole 以及如何修复? 的相关文章

随机推荐

  • Instagram Instagram 标题不起作用

    我有以下用于在 Instagram 上分享文章的代码 void shareInstagram NSURL instagramURL NSURL URLWithString instagram app if UIApplication sha
  • 如何将外部 DOM 附加到 React 组件?

    我有一个页面 其中包含在服务器中呈现的表单 它处理验证以及选择的正确值 我想隐藏该表单的 DOM 并将其附加到 React 组件中 以便我可以在 React router 中使用它 const NewItem React createCla
  • UICollectionView 滚动很慢

    我刚刚创建了一个UICollectionView其中用户可以将手机中的图像添加到应用程序的相册功能中 我将图像保存到文档目录中的子目录中 以便可以添加和删除更多图像 但是 当我上下滚动集合视图时 它非常滞后 怎样才能让滚动条又漂亮又流畅呢
  • “点击恢复”暂停文本 SpriteKit

    我知道 SpriteKit 已经处理了当应用程序进入非活动状态时暂停游戏的问题 但我想做的是在应用程序重新进入活动状态时添加一个 SKLabelNode 点击恢复 现在它正在正确调用我的函数并暂停游戏 但文本没有显示 AppDelegate
  • 具有多个自变量的 Python curve_fit

    蟒蛇的curve fit计算具有单个自变量的函数的最佳拟合参数 但是有没有办法使用curve fit或者其他什么 以适应具有多个自变量的函数 例如 def func x y a b c return log a b log x c log
  • 如何检测实体 EntityState 的更改?

    我想在客户列表的每一行上放置一个 删除 按钮和一个 取消 按钮 当客户 未更改 时 取消 按钮将被禁用 但是 当客户转换到更改状态 已添加 已修改 已删除 时 我想启用 取消 按钮 以便用户可以在保存之前撤消更改 无论它们是什么 我几乎可以
  • 如何防止 PHP DOMDocument“修复”您的 HTML 字符串

    我一直在尝试使用 HTML DOM 对象来解析网页 以便将它们用于应用程序来扫描它们的 SEO 质量 但是我遇到了一些问题 出于测试目的 我编写了一个小型 HTML 页面 其中包含以下不正确的 HTML 正如您所看到的 标题位于 head
  • EF Core 多个导航属性产生循环依赖

    我有以下映射配置 入门级 entity HasOne e gt e CurrentHandling WithOne HasForeignKey
  • Django 多处理和数据库连接

    背景 我正在开发一个使用 Django 和 Postgres 数据库的项目 我们还使用 mod wsgi 以防万一 因为我的一些网络搜索提到了它 在提交 Web 表单时 Django 视图启动一项需要花费大量时间 超过用户想要等待的时间 的
  • 当我们创建空的不可变列表/集合/映射时,是否有任何实际应用/用例

    Java 9 提供了一种创建空不可变列表 集合和映射的方法 List list List of Set set Set of Map map Map of 但我无法理解创建空的不可变列表 集合 映射的实际用例是什么 请帮助我理解空的不可变列
  • 如何在 Zend Framework 2 中引导会话

    在 Zend Framework 2 中启动和运行会话的最佳方法是什么 我尝试过设置session start 在我的 index php 文件中 但随后在引导任何自动加载器之前运行该文件 导致我的会话中存在不完整的对象 在 ZF1 中 您
  • C# - 如何打印宽高比/整页

    我正在单击按钮时打印图表控件 chart1 SaveImage ms ChartImageFormat Bmp Bitmap bm new Bitmap ms PrintDocument doc new PrintDocument doc
  • 如何覆盖 setup.py 默认使用的编译器 (GCC) 标志?

    我明白那个setup py使用相同的CFLAGS用于构建 Python 我有一个我们的 C 扩展存在段错误 我需要构建它without O2因为 O2正在优化一些值和代码 以便核心文件不足以确定问题 我只需要修改setup py以便 O2未
  • 如何从 Java 中的 URL 读取图像?

    我的 Web 应用程序中有提供图像的 servlet 当我使用浏览器图像访问这些 url 时 服务器是正确的 然后我有另一个调整图像大小的 servlet 想法是通过调整大小 servlet 中的 url 访问获取图像 然后调整图像大小 但
  • 最佳实践 - 使用 Symfony 2 删除链接

    在 Symfony 2 中 创建删除记录的链接的最佳方法是什么 我可以定义一条路线 entity delete只接受一个DELETE方法 但我不知道如何创建DELETE来自模板的链接 创建也是同样的道理PUT links 所以你会怎么做 接
  • 如何找到循环图中两个节点之间的最长路径?

    我已经解决了大部分发布的问题here 除了最长的路径之外的所有路径 我读过关于最长路径的维基百科文章 如果图是非循环的 这似乎很容易出现问题 而我的不是 那我该如何解决这个问题呢 通过检查所有可能的路径进行暴力破解 我该如何开始这样做呢 我
  • Microsoft SQL Server“Express”版本可以免费开发吗?

    我可以在 Microsoft SQL Server 2005 2008 Express 版数据库上免费开发软件吗 是的你可以 但 Express 版本有一些限制 SQL Server 一次仅使用一个 CPU 最大内存为1GB 数据库大小限制
  • 为什么 C++ 编译器不优化结构数据成员的读写而不是不同的局部变量?

    我正在尝试创建一些 POD 值的本地数组 例如double 与固定max size在编译时已知 然后读取运行时size value size lt max size 并首先处理size该数组中的元素 问题是 为什么编译器不消除堆栈读写arr
  • Java 位图字体:用不同颜色位图传输 1 位图像

    我想在基于 Java AWT 的应用程序中实现一个简单的位图字体绘制 应用程序借鉴了Graphics对象 我想在其中实现一个简单的算法 1 加载文件 可能使用ImageIO read new File fileName 这是 1 位 PNG
  • 为什么 UserManager.CreateIdentityAsync() 正在寻找 IdentityRole 以及如何修复?

    我在用着身份2 0 with MVC5 代码优先我已经延长了IdentityUser and IdentityRole像这样 public class ApplicationUser IdentityUser Required String