我需要检查用户(连接到项目集合)是否是项目集合的管理员组的成员。另外,我需要为项目做同样的事情。
我需要最正确的方法。
目前我正在使用以下方法,但我不确定它是否是完美的方法。
//Get the IdentityManagementService from the Project Collection
IIdentityManagementService LIdentityService = (IIdentityManagementService)ProjectCollection.GetService(typeof(IIdentityManagementService));
//Search for user identity by user name
TeamFoundationIdentity LUserIdentity = LIdentityService.ReadIdentity(IdentitySearchFactor.AccountName, {Username}, MembershipQuery.Expanded, ReadIdentityOptions.None);
//Get Guid of the same Project Collection to use as scope
String Id = ProjectCollection.InstanceId.ToString();
//Get the Administrators group for Project Collection scope *
TeamFoundationIdentity LAdminIdentity = LIdentityService.ReadIdentity(IdentitySearchFactor.AdministratorsGroup, Id, MembershipQuery.Expanded, ReadIdentityOptions.None);
IsAdmin = false;
//Traverse through the groups of the user and check if administrators group is also one of them
foreach (IdentityDescriptor member in LUserIdentity.MemberOf)
{
if (member.Identifier.Equals(LAdminIdentity.Descriptor.Identifier, StringComparison.CurrentCultureIgnoreCase))
IsAdmin = true;
}
这在一种设置上运行良好,但在 Team Foundation Server 的另一种设置上,在获取管理员组时会出现以下错误。“Team Foundation 身份范围 {项目集合的 Guid} 不存在。”