TFS API 第 29 部分 – TFS 身份模拟





5.00/5 (3投票s)
TFS 身份模拟
什么是 TFS 身份模拟?
TFS 身份模拟是一项在 TFS 2010 中引入的功能,它允许以用户 A 身份运行的进程向 TFS 发送 Web 服务调用,从而使 TFS 认为这些操作是由用户 B 执行的。
很久以前,我写过一篇关于 使用 C# 的访问令牌 的文章,该方法允许您以不同的用户执行操作。
为什么? 在为客户编写工具和服务时,最重要的功能之一是以正确的用户执行操作。 假设我想运行一个服务,每次工作项保存操作时,都希望向某个字段添加值——这个操作很简单,但历史记录将显示来自两个不同用户的两个操作,执行操作的是运行服务的用户,而不是保存工作项的用户。
我还可以找到其他一些身份模拟可以帮助我们的操作。
步骤 1:创建项目并添加引用
创建一个 WPF/WinForm 应用程序并添加以下引用
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Server;
所有文件都位于 - c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\
步骤 2:连接到 Team Foundation Server
(TFS API 第 20 部分:告别 TeamFoundationServer,欢迎 TfsTeamProjectCollection)
TfsTeamProjectCollection tfs;
TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false);
tpp.ShowDialog();
if (tpp.SelectedTeamProjectCollection != null)
{
tfs = tpp.SelectedTeamProjectCollection;
ims = tfs.GetService<IIdentityManagementService>();
GetUsers();
GetAuthenticatedIdentity(tfs);
}
步骤 3:GetUsers & GetAuthenticatedIdentity
我创建了以下方法来简化操作,GetUsers
允许您从列表中选择要模拟的用户,而不是编写帐户名称等。 GetAuthenticatedIdentity
将显示运行 TfsTeamProjectCollection
对象的用户。
/// <summary>
/// Gets all window users from the "Project Collection Valid Users" group
/// </summary>
void GetUsers()
{
IGroupSecurityService gss = (IGroupSecurityService)tfs.GetService(
typeof(IGroupSecurityService));
Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName,
"Project Collection Valid Users", QueryMembership.Expanded);
Identity[] UserId = gss.ReadIdentities(SearchFactor.Sid,
SIDS.Members,QueryMembership.Expanded);
var query = from s in UserId
where s.Type == IdentityType.WindowsUser
select s;
UsersList.ItemsSource = query;
}
/// <summary>
/// Gets the Authorized Identity from the current TFS connection object
/// </summary>
/// <param name="current_tfs">latest tfs connection object</param>
void GetAuthenticatedIdentity(TfsTeamProjectCollection current_tfs)
{
try
{
txt_current_user.Text = current_tfs.AuthorizedIdentity.DisplayName;
}
catch (AccessCheckException ex)
{
//By default, the Administrators group does not have the
//“Make requests on behalf of others” permission
throw new AccessCheckException(ex.Message);
}
}
步骤 4:身份模拟
/// <summary>
/// Impersonation - Creates new instance of TfsTeamProjectCollection object
/// using a different user;
/// </summary>
/// <param name="serverUri">Tfs server uri you want to connect using Impersonation</param>
/// <param name="userToImpersonate">Account name of the user you want to Impersonate</param>
public void Impersonation(Uri serverUri,string userToImpersonate)
{
// Read out the identity of the user we want to impersonate
TeamFoundationIdentity identity = ims.ReadIdentity(IdentitySearchFactor.AccountName,
userToImpersonate,
MembershipQuery.None,
ReadIdentityOptions.None);
tfs_impersonated = new TfsTeamProjectCollection(serverUri, identity.Descriptor);
GetAuthenticatedIdentity(tfs_impersonated);
}
步骤 5:启用“代表他人发出请求”
如果您现在运行代码,**它将失败!**
By default, the Administrators group does not have the “Make requests on behalf of others” permission.
要启用身份模拟,请运行以下命令: tfssecurity.exe /collection:https://:8080/tfs/DefaultCollection /a+ Server FrameworkGlobalSecurity Impersonate adm: ALLOW
运行命令后,您应该看到已启用 “代表他人发出请求”。
要禁用身份模拟,请运行以下命令
tfssecurity.exe /collection:https://:8080/tfs/DefaultCollection /a- Server FrameworkGlobalSecurity Impersonate adm: ALLOW
现在,在启用身份模拟后,您可以使用此工具
Tfs object is now running using “Local Service”
尽情享用!