本文介绍了Visual Studio Team Services Rest API时增强的安全性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用Visual Studio Team Services(Visual Studio Online)公开的Rest API来获取工作项信息。我似乎能够连接,但是当我查看对我的查询的响应时,它是一个带有增强安全错误消息的html页面。我认为这是由于IE中的增强安全性选项,但我从我的客户端机器调用此选项,我只能看到如何在服务器上关闭它的选项。



这是我正在拨打的电话

 使用(var client = new HttpClient())
{
var token =xxxxxxxxxxxx;
var apiVersion =1.0;

var account =xxxxxxxx;
var query =从WorkItems中选择[System.Id],其中[System.WorkItemType] ='WorkItem'顺序由[System.CreatedDate] desc;

var url =https://+ account +。visualstudio.com / Core / _apis / wit /;

//使用(var request = new HttpRequestMessage(HttpMethod.Post,url +wiql))执行返回与指定条件
匹配的工作项ID的查询
{
request.Headers.Add(授权,承载+令牌);
request.Headers.Add(Accept,application / json; api-version =+ apiVersion);

字典<字符串,字符串> body = new Dictionary< string,string>
{
{
查询,查询
}
};

request.Content = new StringContent(JsonConvert.SerializeObject(body),Encoding.UTF8,application / json);

using(var response = await client.SendAsync(request))
{
var content = await response.Content.ReadAsStringAsync();
var workItems = JObject.Parse(content)[workItems]作为JArray;

string [] ids = workItems.Select< JToken,string>(w =>(w [id] +))。Take(10)。ToArray< string>() ;
string idsString = String.Join(,,ids);

//使用(var detailsRequest = new HttpRequestMessage(HttpMethod.Get,url +workitems?ids =+ idsString +& fields = System)获取最后10
的详细信息。 Id,System.Title))
{
detailsRequest.Headers.Add(授权,承载+令牌);
detailsRequest.Headers.Add(Accept,application / json; api-version =+ apiVersion);

using(var detailsResponse = await client.SendAsync(detailsRequest))
{
var detailsContent = await detailsResponse.Content.ReadAsStringAsync();
var detailsWorkItems = JObject.Parse(detailsContent)[value]为JArray;

foreach(动态workItem in detailsWorkItems)
{
Console.WriteLine(工作项:{0}({1}),
workItem.fields [ System.Id],
workItem.fields [System.Title]
);
}
}
}
}
}
}

任何对此的帮助都将不胜感激,



谢谢



Chris

解决方案

您可以将相关网站添加到可信站点(例如:



更新:



基于响应结果,它与增强的安全性无关,结果意味着它未经过身份验证。因此令牌无效,它是OAuth的访问令牌,您需要在将应用注册到VSTS后获取访问令牌。



更多信息,请参阅文章。



文章)

 尝试
{
var username =username;
var password =password;

使用(HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue( 应用程序/ JSON));

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(Basic,
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format) ({0}:{1},用户名,密码))));

使用(HttpResponseMessage响应= client.GetAsync(
https:// {account} .visualstudio.com / DefaultCollection / _apis / build / builds)。结果)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
catch(exception ex)
{
Console.WriteLine(ex.ToString());
}


I'm currently trying to use the Rest APIs exposed by Visual Studio Team Services (was Visual Studio Online) to obtain work item information. I seem to be able to connect however when I look at the response to my query its a html page with a Enhanced Security Error message. I believe that this is due to the Enhanced Security option in IE but I'm calling this from my client machine and I can only see options on how to turn this off on a server.

this is the call i'm making

using (var client = new HttpClient())
        {
            var token =     "xxxxxxxxxxxx";
            var apiVersion = "1.0";

            var account = "xxxxxxxx";
            var query = "Select [System.Id] From WorkItems Where[System.WorkItemType] = 'WorkItem' order by [System.CreatedDate] desc";

            var url = "https://" + account + ".visualstudio.com/Core/_apis/wit/";

            // Execute a query that returns work item IDs matching the specified criteria
            using (var request = new HttpRequestMessage(HttpMethod.Post, url + "wiql"))
            {
                request.Headers.Add("Authorization", "Bearer " + token);
                request.Headers.Add("Accept", "application/json;api-version=" + apiVersion);

                Dictionary<string, string> body = new Dictionary<string, string>
            {
                {
                    "query", query
                    }
            };

                request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");

                using (var response = await client.SendAsync(request))
                {
                    var content = await response.Content.ReadAsStringAsync();
                    var workItems = JObject.Parse(content)["workItems"] as JArray;

                    string[] ids = workItems.Select<JToken, string>(w => (w["id"] + "")).Take(10).ToArray<string>();
                    string idsString = String.Join(",", ids);

                    // Get details for the last 10
                    using (var detailsRequest = new HttpRequestMessage(HttpMethod.Get, url + "workitems?ids=" + idsString + "&fields=System.Id,System.Title"))
                    {
                        detailsRequest.Headers.Add("Authorization", "Bearer " + token);
                        detailsRequest.Headers.Add("Accept", "application/json;api-version=" + apiVersion);

                        using (var detailsResponse = await client.SendAsync(detailsRequest))
                        {
                            var detailsContent = await detailsResponse.Content.ReadAsStringAsync();
                            var detailsWorkItems = JObject.Parse(detailsContent)["value"] as JArray;

                            foreach (dynamic workItem in detailsWorkItems)
                            {
                                Console.WriteLine("Work item: {0} ({1})",
                                    workItem.fields["System.Id"],
                                    workItem.fields["System.Title"]
                                );
                            }
                        }
                    }
                }
            }
        }

any help with this would be appreciated,

thanks

Chris

解决方案

You can add related sites to trusted sites (for example: https://app.vssps.visualstudio.com, https://login.live.com etc…).

  1. Internet option=>Security
  2. Select Trusted sites
  3. Click sites
  4. Type website address and click add

The simple way to know which URLs need to be added, you could send a simple Get Rest request (e.g. get work item REST API), it will pop up a window that contains site URL (will pop up many times for different URL), add these URL to trusted sites list.

Update:

Based on the response result, it isn’t related to enhanced security, the result means it isn’t authenticated. So the token is invalid, it is access token of OAuth, you need to get access token after register your app to VSTS.

More information, you can refer to this article.

There is a OAuth sample that you can refer. After you get access token, add it to request header and retrieve data from VSTS.

If you want to access VSTS through personal access token, the code like this: (check this article)

try
    {
        var username = "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://{account}.visualstudio.com/DefaultCollection/_apis/build/builds").Result)
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }

这篇关于Visual Studio Team Services Rest API时增强的安全性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:43