简体   繁体   English

如何在 Azure Devops 中使用服务帐户模拟用户

[英]How to use service account to impersonate users in Azure Devops

I created a service account to impersonate users in my organization in order to make changes to work items in the users' name.我创建了一个服务帐户来模拟我组织中的用户,以便对用户名下的工作项进行更改。 I already added that service account to the group “Project Collection Service Accounts”, which has “Make requests on behalf of others” set to “Allow”.我已经将该服务帐户添加到“项目集合服务帐户”组中,该组将“代表他人发出请求”设置为“允许”。 The service account has Visual Studio Subscription.服务帐户具有 Visual Studio 订阅。

I then used my code, which is working with our on-premise TFS, to execute the impersonation and I got an error 500 saying that “Access Denied: X needs the following permission(s) to perform this action: Make requests on behalf of others”然后我使用我的代码,它与我们的内部 TFS 一起工作,来执行模拟,我收到错误 500,说“访问被拒绝:X 需要以下权限来执行此操作:代表其他”

What should I do to make it works?我应该怎么做才能使它工作? Here the code I'm using:这是我正在使用的代码:

        var credential = new VssAadCredential("X@myoganization", "password");
        var collection = new TfsTeamProjectCollection(new Uri("my_devops_uri"), credential);
        MyTfsConnection.ProjectCollection = collection;

        MyTfsConnection.IdentityService = collection.GetService<IIdentityManagementService>();
        MyTfsConnection.WIStore = collection.GetService<WorkItemStore>();

        var adAccount = "someone@myoganization";
        var identity = MyTfsConnection.IdentityService.ReadIdentity(IdentitySearchFactor.AccountName, adAccount, MembershipQuery.None, ReadIdentityOptions.None);
        using (var impersonatedCollection = new TfsTeamProjectCollection(new Uri("my_devops_uri"), credential, identity.Descriptor))
        {

            var impersonatedWIStore = impersonatedCollection.GetService<WorkItemStore>();
        }

It's not able to do this, Service account is not intended to connect the client with server either check in code or change work items.它无法做到这一点,服务帐户不打算将客户端与服务器连接起来,无论是签入代码还是更改工作项。

Service account is used to run various services related to TFS.服务帐户用于运行与 TFS 相关的各种服务。 It should have the minimum privileges possible on the machine.它应该具有机器上可能的最低权限。

The client should not connect to the server with a service account, they should be using their own account which you grant access to the relevant repositories in TFS.客户端不应该使用服务帐户连接到服务器,他们应该使用自己的帐户,您授予访问 TFS 中相关存储库的权限。 For example, if you connect all clients with the service account, how will you know who checked in each changeset, who should assign work items to?例如,如果您将所有客户端与服务帐户连接起来您如何知道谁检查了每个变更集,应该将工作项分配给谁?

You will also not able to assign work items to a service account.您也无法将工作项分配给服务帐户。

I've been attempting to migrate code to DevOps Services that uses impersonation to alter work items in another users name as well.我一直在尝试将代码迁移到 DevOps 服务,该服务也使用模拟来更改另一个用户名中的工作项。 Uncanny how similar it is to yours.不可思议的是它和你的有多相似。 It's almost like we all grabbed it from this old post on TFS impersonation .这几乎就像我们都从这篇关于 TFS 模拟的旧帖子中获取的一样 Like yours, this code works with on-premises DevOps Server 2019.1.1 but I ran into the same issue trying to get it working with DevOps Services.像您一样,此代码适用于本地 DevOps Server 2019.1.1,但我在尝试使用 DevOps Services 时遇到了同样的问题。

During my search for an answer that works with the TFS / DevOps (SOAP) client API and DevOps Services, I ran across this SO question where the question quotes the following.在我搜索适用于 TFS/DevOps (SOAP) 客户端 API 和 DevOps 服务的答案期间,我遇到了这个问题,其中问题引用了以下内容。

"For security reasons (and compliance and a number of other reasons), the impersonation header isn't supported on Visual Studio Online" “出于安全原因(以及合规性和许多其他原因),Visual Studio Online 不支持模拟标头”

I've yet to find this same information anywhere in the documentation.我还没有在文档中的任何地方找到相同的信息。 However, it appears to be true.然而,这似乎是真的。 Impersonation is disabled in Azure DevOps Services. Azure DevOps Services 中禁用模拟。 Drat!天啊!

Not giving up, my search also turned up the following from the November 2017 release notes , which appears promising.没有放弃,我的搜索还从2017 年 11 月的发行说明中找到了以下内容,这似乎很有希望。

Grant the bypassrule permission to specific users向特定用户授予绕过规则权限

Often, when migrating work items from another source, organizations want to retain all the original properties of the work item.通常,当从另一个源迁移工作项时,组织希望保留工作项的所有原始属性。 For example, you may want to create a bug that retains the original created date and created by values from the system where it originated.例如,您可能希望创建一个保留原始创建日期并由其起源系统中的值创建的错误。

The API to update a work item has a bypassrule flag to enable that scenario.用于更新工作项的 API 有一个绕过规则标志来启用该场景。 Previously the identity who made that API request had to be member of the Project Collection Administrators group.以前,提出该 API 请求的身份必须是 Project Collection Administrators 组的成员。 With this deployment we have added a permission at the project level to execute the API with the bypassrule flag.通过此部署,我们在项目级别添加了一个权限,以使用 bypassrule 标志执行 API。

However, I find no such permission in the collection / organization or project permissions on either DevOps Server or DevOps Services.但是,我发现 DevOps Server 或 DevOps Services 的集合/组织或项目权限中没有此类权限。 Drat again!再打! That led me to this SO answer where a work item update is crafted in JSON using the REST API directly.这让我找到了这个 SO 答案,其中直接使用 REST API 在 JSON 中制作了工作项更新。 So the bypass rule option must still be valid, just not exposed as a settable permission.所以绕过规则选项必须仍然有效,只是不作为可设置的权限公开。

So I started looking at the TFS / DevOps (SOAP) client API again and found the WorkItemStoreFlags.BypassRules flag to pass to the WorkItemStore when it is created.所以我再次开始查看 TFS/DevOps (SOAP) 客户端 API,发现WorkItemStoreFlags.BypassRules标志在创建时传递给WorkItemStore The following should provide the basic mechanics.下面应该提供基本的机制。

// Use a personal access token with Work Items scope
var credentials = new VssBasicCredential(String.Empty, "Your PAT");

// Connect with TFS / DevOps client libs.
// Older SOAP based client but still works with DevOps Services.
var teamProjectCollection = new TfsTeamProjectCollection(new Uri("https://dev.azure.com/your-org"), credentials);

// Don't use teamProjectCollection.GetService<WorkItemStore>().  
// New up WorkItemStore using the collection and explicitly specify the BypassRules flag.
// This allows you to set the CreatedBy field later.
var workItemStore = new WorkItemStore(teamProjectCollection, WorkItemStoreFlags.BypassRules);

// Get the project, work item type, and create the new work item.
var project = workItemStore.Projects["YourProject"];
var workItemType = project.WorkItemTypes["Product Backlog Item"];
var workItem = new WorkItem(workItemType);

// Set the work item fields
workItem.Title = "The Title";
// Without the BypassRules flag the CreatedBy value set here will be ignored on
// Save() and replaced with the user account attached to the PAT used to authenticate.
workItem.Fields[CoreField.CreatedBy].Value = "NotYourUser@yourdomain.com";
workItem.Fields[CoreField.AssignedTo].Value = "NotYourUser@yourdomain.com";
workItem.Save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SharePoint2010上将我的帐户模拟为服务帐户 - How to impersonate my account to service account on SharePoint2010 模拟用户电子邮件到Google服务帐户 - Impersonate an user email to google service account 如何在许多模拟单个 AD 用户帐户的网络 PC 上创建 C# 服务 - How to create a C# service on many network PC's that all impersonate a single AD user account 使用服务帐户读取/更新O365用户的日历 - Use a service account to read/update calendars of O365 users 如何以静默方式获取Azure DevOps的用户令牌并将其用于访问DevOps REST API? - How to get user token silently for Azure DevOps and use it for accessing DevOps REST APIs? 在Windows服务中模拟用户 - Impersonate user in Windows Service 同时模拟多个用户 - impersonate multiple users at the same time 如何从Azure云应用服务获取用户IP - How to get users IP from Azure cloud app service 如何在ASP.NET MVC网站上为用户使用Windows身份验证,以及如何在后端WCF服务上使用AppPool用户模拟用户? - How can I use Windows Authentication for Users on a ASP.NET MVC site and impersonate the user using the AppPool user on back-end WCF services? “密钥无法在指定状态下使用”:如何加载用户的个人资料以进行模拟? - “Key not valid for use in specified state”: How to load profile of user to impersonate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM