You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ABP Framework:如何从IIdentityUserRepository获取自定义IsPartner属性用户

解决方案

要获取包含自定义IsPartner属性的合作伙伴用户列表,你可以从以下几个方向处理:

1. 确认已正确扩展IdentityUser实体

首先确保你已扩展ABP的IdentityUser类,将IsPartner作为实体字段添加:

public class AppUser : IdentityUser
{
    public bool IsPartner { get; set; }
}

同时在你的DbContext中配置该实体,执行数据库迁移,确保IsPartner字段同步到数据库表中。

2. 使用扩展后的用户类型仓储

默认IIdentityUserRepository返回基础IdentityUser类型,不包含自定义属性。你需要注入针对扩展后AppUser的仓储:

public class UtilityAppService : CrmAppService, IUtilityAppService
{
    private readonly IRepository<AppUser, Guid> _userRepository;

    public UtilityAppService(IRepository<AppUser, Guid> userRepository)
    {
        _userRepository = userRepository;
    }

    public async Task<List<AppUser>> GetPartnerUsersAsync()
    {
        // 直接筛选IsPartner为true的用户
        return await _userRepository.GetListAsync(u => u.IsPartner);
    }
}

3. 若自定义属性以Claim形式存储

如果IsPartner是通过用户Claim添加的(而非数据库实体字段),查询时需要包含Claims并筛选:

public async Task<List<IdentityUser>> GetPartnerUsersAsync()
{
    var allUsers = await _repository.GetListAsync(include: u => u.Include(u => u.Claims));
    return allUsers.Where(u => u.Claims.Any(c => 
        c.ClaimType == "IsPartner" && c.ClaimValue.Equals("true", StringComparison.OrdinalIgnoreCase)
    )).ToList();
}

4. 使用IdentityUserManager查询

也可以注入IdentityUserManager<AppUser>直接访问用户集合:

public class UtilityAppService : CrmAppService, IUtilityAppService
{
    private readonly IdentityUserManager<AppUser> _userManager;

    public UtilityAppService(IdentityUserManager<AppUser> userManager)
    {
        _userManager = userManager;
    }

    public async Task<List<AppUser>> GetPartnerUsersAsync()
    {
        return await _userManager.Users.Where(u => u.IsPartner).ToListAsync();
    }
}

内容的提问来源于stack exchange,提问作者Muhammad Jahangir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 00:01:03