TFS API:如何检查用户是否具有项目集合和项目的管理员权限?

时间:2013-06-10 12:16:55

标签: tfs tfs2012 tfs-sdk

我需要检查用户(连接到Project Collection的用户)是否是Project Collection的administrators组的成员。另外,我需要为项目做同样的事情。

我需要最正确的方法。

目前我正在使用以下方法,但我不确定它是否是完美的方式。

//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的另一个设置上,它在获取管理员组时出现以下错误。 “团队基金会身份范围{项目集合的指导}}不存在。”

0 个答案:

没有答案