简体   繁体   English

如何使用 c# API 授权 Azure Devops?

[英]How to Authorize Azure Devops using c# API?

I have installed "Microsoft.TeamFoundationServer.Client" package in VS and creating DevOps Work item forms.我已经在 VS 中安装了“Microsoft.TeamFoundationServer.Client”package 并创建了 DevOps 工作项 forms。 I want to add custom Rules to the work item form using below code.我想使用以下代码将自定义规则添加到工作项表单。 When running below code I am getting the 'result' value as html content在以下代码下运行时,我将“结果”值作为 html 内容

output: "Azure DevOps Services | Sign In" output:“Azure DevOps 服务 | 登录”

Code:
     var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://dev.azure.com/orgname/_apis/work/processes/processid/workItemTypes/Microsoft.VSTS.WorkItemTypes.Epic/rules?api-version=6.0-preview.2");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "POST";            
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = @"{ 
                                'name': 'customRule',
                              'conditions': [
                                {
                                  'conditionType': '$when',
                                  'field': 'Custom.parentdrpdwn',
                                  'value': 'value1'
                                }
    
                              ],
                              'actions': [
                                {
                                  'actionType': '$setDefaultValue',
                                  'targetField': 'Custom.childrpdwn',
                                  'value': 'value2'
                                }
                              ],
                              'isDisabled': false
                        }";
                    streamWriter.Write(json);
                }
    
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                } 
 

because of this authentication issue I am not able to post the https request,how can we authorize the Azure DevOps in the same method and create custom business rule in my Org?由于此身份验证问题,我无法发布 https 请求,我们如何以相同的方法授权 Azure DevOps 并在我的组织中创建自定义业务规则?

Azure DevOps API uses basic auth. Azure DevOps API 使用基本身份验证。 You just need to add a header:您只需要添加一个 header:

byte[] bytes = System.Text.Encoding.ASCII.GetBytes("username:password");
string patEncoded = Convert.ToBase64String(bytes);
httpWebRequest.Headers.Add("Authorization", "Basic " + patEncoded);

You can use your username + password or generate a personal access token (PAT).您可以使用您的用户名 + 密码或生成个人访问令牌 (PAT)。 For a PAT, leave the username empty.对于 PAT,将用户名留空。

We need to create PAT token in the web page and then use it in the code.我们需要在 web 页面中创建 PAT 令牌,然后在代码中使用它。 Here is an sample.这是一个示例。

string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));

We could refer to the doc Get started sample and Get started with the REST APIs for more details.我们可以参考文档入门示例REST API 入门了解更多详细信息。

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

相关问题 C# Azure Devops API Git:如何使用 C# API 推送文件 - C# Azure Devops API Git: How to push files using the C# API 如何使用 API 和 c# 从 azure DevOps 下载文件到指定路径 - How download file form azure DevOps sever to a specified path using API and c# Azure Devops REST API 与 ZD7EFA19FBE7D3972FD5ADB6024223D7 - Azure Devops REST API with C# Authentication 如何使用 C# 为 azure devops 变量赋值 - How to assign value to azure devops variable using C# How to call AZURE DEVOPS rest API in the water fall dialog built using C# BOT Framework SDK V4? - How to call AZURE DEVOPS rest API in the water fall dialog built using C# BOT Framework SDK V4? 使用 Azure DevOps 创建工作项 Rest API 使用 ZD7EFA19FBE7D2372FD5ADB6024 - Create Work Item using Azure DevOps Rest API using C# 使用 REST API (C#) 在带有附件的 Azure DevOps 中创建问题(工作项) - Create an Issue (WorkItem) in Azure DevOps with Attachments using a REST API (C#) 使用 C# 通过 API 在 Azure DevOPs 中创建高度自定义的工作项 - Creating a highly customized Work Item in Azure DevOPs through the API using C# 使用 azure devops API 在 C# 中更新发布和构建定义中的代理池 - Updating agent pools in release and build definitions using the azure devops API in C# C# Azure DevOps API 检索所有项目 - C# Azure DevOps API retrieve all projects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM