实现此目的的一种方法可能是让每个用户(父母)Claims代表他们的每个孩子的亲子关系。这些将被添加到用户的主体in the manner described in this article。某些数据存储(例如数据库表)会记录哪些用户(父母)是哪些子女的父母。最后,在API操作方法中,将请求信息的孩子的ID与用户主体上的子级声明列表进行比较,如果该ID不存在,则返回4xx状态码(可能为403 Forbidden
)。
喜欢的东西:
// Cast the Thread.CurrentPrincipal
IClaimsPrincipal icp = Thread.CurrentPrincipal as IClaimsPrincipal;
// Access IClaimsIdentity which contains claims
IClaimsIdentity claimsIdentity = (IClaimsIdentity)icp.Identity;
// Access claims
if(claimsIdentity.Claims
.Where(c => c.ClaimType == "name-of-your-custom-claim")
.Select(c => c.Resource.ToString())
.Contains(childId)) { return new HttpResponseMessage(HttpStatusCode.Forbidden);
应该工作。没有测试代码或检查它编译;这是给出要点的例子。我可能会在稍后清理它。