c# - UserManager.IsInRoleAsync 和 UserManager.GetRolesAsync 彼此不一致

标签 c# asp.net-core-identity

突然,对 UserManager.IsInRoleAsync 的查询在应返回“true”时却为用户返回“false”。查询用户的 UserManager.GetRolesAsync 确认 UserManager.IsInRoleAsync 确实应该返回“true”。
下面是调试器的屏幕截图,说明了问题:

enter image description here

我完全困惑了。 我正在使用 Microsoft.Extensions.Identity.Core (2.2.0.0)。

最佳答案

根据我的经验,这可能是由以下两个问题之一引起的:

  1. 我相信 IsInRoleAsync 要求用户经过身份验证才能工作。

  2. 如果您从旧的 ASP.NET 成员资格架构迁移角色,则 Microsoft 提供的用于将数据迁移到 ASPNetRoles 表的脚本不会填充 NormalizedName 列。 IsInRoleAsync 正在对照此字段检查角色。填充该字段,它应该返回 true。

关于c# - UserManager.IsInRoleAsync 和 UserManager.GetRolesAsync 彼此不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61807503/

相关文章:

c# - 如何在 WPF 中使用不同的坐标系? (仅限缩放)

c# - 有没有办法导致传递给 Control.BeginInvoke 的委托(delegate)的类型推断?

c# - 带有 C# 的 XNA 中的 NullReferenceException

c# - System.Timers.Timer 奇怪的行为?被阻止的事件不会被堆叠

c# - ASP.NET Core Identity 模拟特定用户

c# - IdentityUser 的 IUserValidator

c# - Asp Core 2.1 Jwt + 身份。 userManager 存储未实现 IUserRoleStore

c# - 如何阻止自定义 ThrowHelper 出现在您的 StackTrace 中?

c# - 根据 StartUp.cs 设置,Cookie 不会过期

c# - Asp.Net Core Identity - 创建自定义 token 提供者