如何在Microsoft Identity 3中访问当前用户以外的用户的声明 [英] How to access claims of other than current user in Microsoft Identity 3
问题描述
我正在使用Microsoft Identity3.我可以访问当前用户的声明. 但是我不知道如何为另一个用户做同样的事情.
I'm using Microsoft Identity 3. I can access the claims of the current user. But I can't figure out how to do the same for another user.
对于当前用户,在控制器内部,我可以通过以下方式访问Claims集合:
For the current user, inside the controller, I can access the claims collection via:
IEnumerable<Claim> claims = User.Claims;
我可以看到所有用户声明,并且可以添加如下声明:
I can see all the user claims and I can add a claim as follows:
var user = await GetCurrentUserAsync();
await _userManager.AddClaimAsync(user, new Claim("role", "manager"));
但是,如果我这样做:
IdentityUser user = await _userManager.FindByIdAsync(userid);
用户"有一个声明"集合,但计数为零,该集合为空. 如何访问当前用户以外的其他人的声明,并能够添加和删除声明?
"user" has a "Claims" collection but the count is zero and the collection is empty. How can I access the claims of other than the current user and be able to add and delete claims?
推荐答案
即使我不完全了解为什么",解决我想做的事情也很容易.我可以在以下方法中将IdentityUser转换为ApplicationUser,并且可以使用:
The solution to what I wanted to do turned out to be very easy -- even though I don't fully understand the "why". I can just up-cast IdentityUser to ApplicationUser in the following and it works:
IdentityUser user = await _userManager.FindByIdAsync(userId);
// The following will show the current claims:
var claims = await _userManager.GetClaimsAsync((ApplicationUser) user);
// The following adds a new claim:
await _userManager.AddClaimAsync((ApplicationUser) user, new Claim("time", "yesterday"));
这篇关于如何在Microsoft Identity 3中访问当前用户以外的用户的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!