c# - userManager.GetRolesAsync(user) 扩展 IdentityRole 后返回空列表
问题描述
我需要向 IdentityRole 添加一个 List 属性以建立多对多关系。完成此操作并运行代码优先迁移后,一切似乎都很好。但是,现在当我尝试为用户提取角色时,即使我可以看到它们存在于数据库中,结果也始终为空。知道为什么会发生这种情况吗?
public class Role : IdentityRole
{
public Role(string name) : base(name) { }
public List<FeatureRole> FeatureRoles { get; set; }
}
public class FeatureRole
{
public int FeatureId { get; set; }
public Feature Feature { get; set; }
public string RoleId { get; set; }
public Role Role { get; set; }
}
public class Feature
{
public int Id { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public Permission AvailablePermissions { get; set; }
public Permission Permissions { get; set; }
public bool IsSystem { get; set; }
public List<FeatureRole> FeatureRoles { get; set; }
}
public async Task<CurrentUserJS> AuthenticateAsync(string username, string password)
{
try
{
var user = await _userManager.FindByNameAsync(username);
if(user != null)
{
var signInResult = await _signInManager.PasswordSignInAsync(user, password, false, false);
if(signInResult.Succeeded)
{
var roles = await _userManager.GetRolesAsync(user); // <-- roles = [] always
//....
}
解决方案
推荐阅读
- vb.net - 如何将两个ListBox的项目(代表数字)的值相加
- python - 从单个属性中提取多个文本数据
- apache-spark - Spark - 如何重命名orc文件(不是表)中的列
- c++ - 将指针传递给导致第二次调用使程序崩溃的函数
- python - TypeError:无法根据规则“安全”使用 hvPlot 和 datashade 将数组数据从 dtype('int64') 转换为 dtype('int32')
- javascript - 每次进入屏幕时重新获取数据
- c# - 无法使用 JWT 方法(C#)生成新令牌
- dataframe - 使用分区批处理过程生成多个列,无需洗牌或自定义连接逻辑
- java - 如何将当前日期时间小时秒保存为字符串,以便我可以一次又一次地重复使用它
- git - Git分支-b“分支名称”从master创建?