Asp.net core 获取具有给定声明的所有用户

Asp.net core 获取具有给定声明的所有用户,asp.net-core,asp.net-identity,entity-framework-core,claims-based-identity,Asp.net Core,Asp.net Identity,Entity Framework Core,Claims Based Identity,我正在从事一个aspnet core 1.0项目,最近我了解了什么是声明,以及如何使用它们来实现基于策略的授权 我想说的是: 我希望获得使用给定策略授权的所有用户(即具有给定声明的用户,比如claim{Type=“CanReceiveXReport”,Value=True}) 我希望这样做的代码如下所示: public static IQueryable<User> WithClaim(this IQueryable<User> users, string claimNa

我正在从事一个aspnet core 1.0项目,最近我了解了什么是声明,以及如何使用它们来实现基于策略的授权

我想说的是: 我希望获得使用给定策略授权的所有用户(即具有给定声明的用户,比如
claim{Type=“CanReceiveXReport”,Value=True}

我希望这样做的代码如下所示:

public static IQueryable<User> WithClaim(this IQueryable<User> users, string claimName) =>
    users
        .Include(u=> u.Claims)
        .Where(u=> u.Claims.Any(c=> c.ClaimType == claimName));

我相信您正在UserManager中寻找方法:

/// <summary>
/// Returns a list of users from the user store who have the specified <paramref name="claim"/>.
/// </summary>
/// <param name="claim">The claim to look for.</param>
/// <returns>
/// A <see cref="Task{TResult}"/> that represents the result of the asynchronous query, a list of <typeparamref name="TUser"/>s who
/// have the specified claim.
/// </returns>
public virtual Task<IList<TUser>> GetUsersForClaimAsync(Claim claim)
{
    ThrowIfDisposed();
    var store = GetClaimStore();
    if (claim == null)
    {
        throw new ArgumentNullException(nameof(claim));
    }
    return store.GetUsersForClaimAsync(claim, CancellationToken);
}
//
///返回用户存储中具有指定权限的用户列表。
/// 
///要寻找的索赔。
/// 
///表示异步查询结果的,是
///拥有指定的索赔。
/// 
公共虚拟任务GetUsersForcAimAsync(声明声明)
{
ThrowIfDisposed();
var store=GetClaimStore();
如果(索赔==null)
{
抛出新的ArgumentNullException(nameof(claim));
}
return store.getusersforclaimsync(claim,CancellationToken);
}

是的,我也找到了这个方法,但是我需要用索赔值限制我的搜索,这对我的特殊情况不起作用。我查看了该方法的实现,并且正在编写忽略声明值的自己的版本。
/// <summary>
/// Returns a list of users from the user store who have the specified <paramref name="claim"/>.
/// </summary>
/// <param name="claim">The claim to look for.</param>
/// <returns>
/// A <see cref="Task{TResult}"/> that represents the result of the asynchronous query, a list of <typeparamref name="TUser"/>s who
/// have the specified claim.
/// </returns>
public virtual Task<IList<TUser>> GetUsersForClaimAsync(Claim claim)
{
    ThrowIfDisposed();
    var store = GetClaimStore();
    if (claim == null)
    {
        throw new ArgumentNullException(nameof(claim));
    }
    return store.GetUsersForClaimAsync(claim, CancellationToken);
}