简体   繁体   English

TFS REST API授权

[英]TFS REST API Authorization

Getting "Unauthorized" status code when requesting TFS REST API via C# Console Application and TFS 2015 Update 3. 通过C#控制台应用程序和TFS 2015 Update 3请求TFS REST API时获取“未经授权”状态代码。

Hello everybody, 大家好,

I'm trying to control the TFS (2015 Update 3) REST API with a C# console application. 我正在尝试使用C#控制台应用程序控制TFS(2015更新3)REST API。

In my old application I referenced the Microsoft.VisualStudio.Build.Client (etc.) libraries and could access the information I needed via the TfsTeamProjectCollection . 在我的旧应用程序中,我引用了Microsoft.VisualStudio.Build.Client (等)库,并且可以通过TfsTeamProjectCollection访问所需的信息。 I honestly don't know where the authentication comes from. 老实说,我不知道身份验证来自哪里。 Maybe via the Windows credentials or the account associated with Visual Studio. 可能通过Windows凭据或与Visual Studio关联的帐户。 In any case, I didn't have to specify credentials in my application. 无论如何,我都不必在应用程序中指定凭据。

But I didn't find an example for the REST API where I don't have to specifically authorize. 但是我没有找到不需要专门授权的REST API示例。

I found an example of using the .NET client library that authorizes itself with VssCredentials (using NTLM). 我找到了一个使用.NET客户端库的示例,该客户端库通过VssCredentials(使用NTLM)对其进行了授权。 I don't have to specify a username or oassword here either: 我不必在此处指定用户名或oassword:

VssConnection connection = new VssConnection(new Uri(URL), new VssCredentials());

That works too, but I can't get all the information I need about it, so I wanted to control the REST API myself. 这也可行,但是我无法获得所需的所有信息,因此我想自己控制REST API。

Can someone send me an example of how I can access the REST API without a separate username/password? 有人可以给我发送一个示例,说明我如何在没有单独的用户名/密码的情况下访问REST API吗? Or is this no longer possible? 还是不再可能?

Thank you Johannes 谢谢约翰尼斯

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    using (HttpResponseMessage response = await client.GetAsync("http://ServerName:8080/tfs/CollectionName/ProjectName/_apis/build/builds/BuildID"))
    {
        response.EnsureSuccessStatusCode();
        string responseBody = await response.Content.ReadAsStringAsync();
        Console.WriteLine(responseBody);
    }
}

I expect to get the json response but instead I only get the Unathorized Exception. 我希望得到json响应,但我只会得到Unathorized Exception。

I also use HttpClient with pat or username/password. 我还将HttpClient与pat或用户名/密码一起使用。 I have tested with default credentials and I have got 401. Then I found this post: How to get HttpClient to pass credentials along with the request? 我已经使用默认凭据进行了测试,并且已经获得401。然后我发现了这篇文章: 如何使HttpClient随请求一起传递凭据?

I`ve created the sample with WebClient and It works: 我已经使用WebClient创建了示例,并且可以正常工作:

WebClient wc = new WebClient();
wc.UseDefaultCredentials = true;
wc.Headers.Add("Content-Type", "application/json");

var data = wc.OpenRead("http://my-srv:8080/tfs/DefaultCollection/MyProject/_apis/build/builds/MyId?api-version=3.0");

var response = new StreamReader(data).ReadToEnd();

You need to add HttpClientHandler() to your HttpClient : 您需要将HttpClientHandler()添加到您的HttpClient

using (HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }))
{
     // ... 
}

Just like Shamrai Aleksander said you can use WebClient default credentials,or,You can create a custome PAT using TFS Security and limit user access. 就像Shamrai Aleksander所说的那样,您可以使用WebClient默认凭据,或者,可以使用TFS Security创建自定义PAT并限制用户访问。 Please find more about TFS PAT here : https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops 请在这里找到有关TFS PAT的更多信息: https ://docs.microsoft.com/zh-cn/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view = azure-devops

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM