[英]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.