如何以编程方式将用户添加到 TFS

发布于 2024-12-13 23:14:09 字数 553 浏览 5 评论 0原文

我想以编程方式将用户添加到团队项目中。我发现的解决方案是这样的:

IGroupSecurityService gss = (IGroupSecurityService)objTFS.GetService(typeof(IGroupSecurityService));
Identity identity = gss.ReadIdentity(SearchFactor.AccountName, "Group name", QueryMembership.None);
gss.AddMemberToApplicationGroup(groupProject.Sid, member.Sid);

但这仅适用于 TFS 已知的组/用户。

我想将 Windows 帐户添加到 TFS

例如:

Windows 帐户名称:TestTFS

密码:123456

然后以编程方式将 TestTFS 添加到 TFS。

我知道有一个名为 TeamFoundation Administration Tool 的工具可以做到这一点,但我不想使用它。

I want to add users programmatically to a Team Project. What I found out to be the solution was this:

IGroupSecurityService gss = (IGroupSecurityService)objTFS.GetService(typeof(IGroupSecurityService));
Identity identity = gss.ReadIdentity(SearchFactor.AccountName, "Group name", QueryMembership.None);
gss.AddMemberToApplicationGroup(groupProject.Sid, member.Sid);

But this only work for groups/users known to TFS.

I want to add a Windows account to TFS

For example:

Windows account name: TestTFS

Password:123456

Then add the TestTFS to TFS programmatically.

I know there is a tool named TeamFoundation Administration Tool can do that, but I do not want to use it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人练习一个人 2024-12-20 23:14:09

在 TFS2012 中,IGroupSecurityService 被标记为过时,并替换为 IIdentityManagementService

您可以使用 IIdentityManagementService.ReadIdentity()以及 IIdentityManagementService.AddMemberToApplicationGroup() 将 Windows 用户添加到 TFS 组,即使 TFS 尚不知道这些 Windows 用户。

这是通过指定 ReadIdentityOptions 来完成的。 IncludeReadFromSource 选项。

以下是将 Windows 用户 VSALM\Barry 添加到 FabrikamFiber 团队项目中的 Fabrikam Fiber Web 团队(TFS 组)的示例,在 http://vsalm:8080/tfs/FabrikamFiberCollection 服务器/集合中。

您需要添加对:Microsoft.TeamFoundation.ClientMicrosoft.TeamFoundation.Common 的引用

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using System;

namespace ConsoleApplication1
{
   class Program
        {
        static void Main(string[] args)
        {
            var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://vsalm:8080/tfs/FabrikamFiberCollection"));

            var ims = tpc.GetService<IIdentityManagementService>();

            var tfsGroupIdentity = ims.ReadIdentity(IdentitySearchFactor.AccountName,
                                                    "[FabrikamFiber]\\Fabrikam Fiber Web Team",
                                                    MembershipQuery.None,
                                                    ReadIdentityOptions.IncludeReadFromSource);            

            var userIdentity = ims.ReadIdentity(IdentitySearchFactor.AccountName,
                                                    "VSALM\\Barry",
                                                    MembershipQuery.None,
                                                    ReadIdentityOptions.IncludeReadFromSource);

            ims.AddMemberToApplicationGroup(tfsGroupIdentity.Descriptor, userIdentity.Descriptor);
        }
    }
}

In TFS2012, the IGroupSecurityService was marked obsolete and replaced with IIdentityManagementService.

You can use IIdentityManagementService.ReadIdentity() along with IIdentityManagementService.AddMemberToApplicationGroup() to add Windows users to TFS groups, even if those Windows users are not known to TFS yet.

This is accomplished by specifying the ReadIdentityOptions.IncludeReadFromSource option.

Below is an example of adding a Windows user VSALM\Barry to the Fabrikam Fiber Web Team (TFS Group), in the FabrikamFiber Team Project, in the http://vsalm:8080/tfs/FabrikamFiberCollection server/collection.

You will need to add references to: Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.Common

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using System;

namespace ConsoleApplication1
{
   class Program
        {
        static void Main(string[] args)
        {
            var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://vsalm:8080/tfs/FabrikamFiberCollection"));

            var ims = tpc.GetService<IIdentityManagementService>();

            var tfsGroupIdentity = ims.ReadIdentity(IdentitySearchFactor.AccountName,
                                                    "[FabrikamFiber]\\Fabrikam Fiber Web Team",
                                                    MembershipQuery.None,
                                                    ReadIdentityOptions.IncludeReadFromSource);            

            var userIdentity = ims.ReadIdentity(IdentitySearchFactor.AccountName,
                                                    "VSALM\\Barry",
                                                    MembershipQuery.None,
                                                    ReadIdentityOptions.IncludeReadFromSource);

            ims.AddMemberToApplicationGroup(tfsGroupIdentity.Descriptor, userIdentity.Descriptor);
        }
    }
}
我们的影子 2024-12-20 23:14:09

我遇到了同样的问题,最后以下代码起作用了

            NTAccount f = new NTAccount(userName);
            SecurityIdentifier s = (SecurityIdentifier)f.Translate(typeof(SecurityIdentifier));
            string userSid = s.ToString();

命名空间:using System.Security.Principal;

I had the same issue, finally following code worked

            NTAccount f = new NTAccount(userName);
            SecurityIdentifier s = (SecurityIdentifier)f.Translate(typeof(SecurityIdentifier));
            string userSid = s.ToString();

Namespace : using System.Security.Principal;

ㄖ落Θ余辉 2024-12-20 23:14:09

为了通过 TFS API 执行此操作,您需要访问 2 个级别的信息。

  1. 您要添加的用户的sid,

  2. 您要添加用户的组的sid
    =>您在帖子中显示的代码将获取您想要将用户添加到的组的 id。

我找到了 2 个包含示例代码的链接,我认为对您有帮助,

In order to perform this operation via the TFS API, you need access to 2 levels of information.

  1. The sid of the user you want to add,

  2. The sid of the group you want to add the user to
    => The code you have shown in your post will get you the id of the group you want to add the user to.

I found 2 links with sample code i think can be helpful to you,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文