TFS 2013.4内部部署。启用基本身份验证以访问Tfs REST Api [英] TFS 2013.4 On-Premise. Enable Basic Authentication to access Tfs REST Api
问题描述
我正在尝试使用ajax访问tfs rest api,但我遇到基本身份验证配置不正确。
API说明 - http://www.visualstudio.com/en-us/integrate/reference/reference-vso-overview-vsi
请您建议正确的方法访问REST API(tfs 2013.4内部部署)?
我试过Visual Studio Online - 工作正常。
IIS设置:
- 基本认证 - 服务角色已安装并启用。
- Windows身份验证(NTLM) - 已启用
- Anonymous
身份验证 - 已启用
C#示例:
HttpRequestException - 响应状态代码并不表示成功:401(未经授权)
var username =" Domain\\UserName" ;;
var password =" Password" ;;
using(HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(" application / json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue (" Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format(" {0}:{1}",username,密码))));
使用(HttpResponseMessage response = client.GetAsync(
" https:// {tfs_url} /_apis/projects?api-version=1.0").Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
即使在TFS更新到第4版后,工作项也无法进行跟踪。
谢谢
您好Olexiy,
我找到了一个使用Newtonsoft.Json和RestSharp的解决方案。
我的示例代码如下所示:
使用RestSharp;
使用System;
使用System.Net;
namespace RestTest01
{
class program
{
static void Main(string [] args)
{
RestClient a_client = new RestClient(" http:// TFSSERVER:8080 / tfs / DefaultCollection");
RestRequest a_request = new RestRequest(" / _ apis / wit / workitems /?ids = 123& api-version = 1.0-preview",Method.GET);
a_request.Credentials = new NetworkCredential(@"Domain\User"," Password");
var a_response = a_client.Execute(a_request);
Console.WriteLine(a_response.StatusCode);
}
}
}此示例将按示例ID获取工作项123.重要的是NetworkCredentials。如果没有这个,身份证不起作用。
祝你好运,
 阿明
Hi,
I'm trying to access tfs rest api with using ajax, but I faced that Basic authentication is not configured correctly.
API description - http://www.visualstudio.com/en-us/integrate/reference/reference-vso-overview-vsi
Could you please suggest the right way to access REST api (tfs 2013.4 on-premise)?
I tried Visual Studio Online - works fine.
IIS Settings:
- Basic Authentication - service role is installed and enabled.
- Windows Authentication (NTLM) - enabled
- Anonymous Authentication - enabled
C# example:
HttpRequestException - Response status code does not indicate success: 401 (Unauthorized)
var username = "Domain\\UserName"; var password = "Password"; using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Add( new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String( System.Text.ASCIIEncoding.ASCII.GetBytes( string.Format("{0}:{1}", username, password)))); using (HttpResponseMessage response = client.GetAsync( "https://{tfs_url}/_apis/projects?api-version=1.0").Result) { response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } }
Also Work Item Tracing is not available even after TFS Update to v.4
Thanks
Hi Olexiy,
I found a solutions, using Newtonsoft.Json and RestSharp.
My sample code looks like this:
using RestSharp; using System; using System.Net; namespace RestTest01 { class Program { static void Main(string[] args) { RestClient a_client = new RestClient("http://TFSSERVER:8080/tfs/DefaultCollection"); RestRequest a_request = new RestRequest("/_apis/wit/workitems/?ids=123&api-version=1.0-preview", Method.GET); a_request.Credentials = new NetworkCredential(@"Domain\User", "Password"); var a_response = a_client.Execute(a_request); Console.WriteLine(a_response.StatusCode); } } }This code will get a work item by id, in the example 123. Important are the NetworkCredentials. Without this, id doesn't work.
Best regards,
Armin
这篇关于TFS 2013.4内部部署。启用基本身份验证以访问Tfs REST Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!