简体   繁体   English

在Azure Devops Rest API中授权

[英]Authorizing in azure devops rest API

I have a use a azure devops api for statistics but i cant authenticate in c# when sending header. 我使用azure devops api进行统计,但是发送标头时无法在c#中进行身份验证。

i use webrequests. 我使用webrequests。

after a bunch of testing i went and used the postman to test it out and i could get in using username and password so i coppied that authorization headear which 100% works since i did get information back from server. 经过一堆测试后,我去了邮递员对其进行测试,我可以使用用户名和密码进入,所以我复制了授权头饰,因为我确实从服务器取回了信息,所以这种授权头是100%有效的。

var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "GET";

            httpWebRequest.Headers.Add("Authorization:Basic base64Username:password");
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
                Console.WriteLine(result);
            }

in c# all im getting in response is whole bunch of html type code what i guess is part of authentication website 在C#中,所有即时通讯都会得到响应,这是一堆完整的html类型代码,我猜这是身份验证网站的一部分

The fact that you're getting a bunch of HTML back suggests that you're not forming the basic auth header in the correct way, or that the username you're providing is not recognised. 返回大量HTML的事实表明您没有以正确的方式形成基本的auth标头,或者无法识别您提供的用户名。 It's not clear from your code sample which it is, but if you were supplying a correctly formed auth header, with invalid credentials, you would receive a 401 Unauthorized response, which the HttpWebRequest class would throw as an exception. 从代码示例中还不清楚这是什么,但是如果您提供的格式正确的auth头具有无效的凭据,则会收到401 Unauthorized响应, HttpWebRequest类将其作为异常抛出。

Two things to check: 检查两件事:

  • Ensure you're forming the auth header correctly (this is almost certainly it, it's not clear from your question how you copied it from postman or tried generating it in C#) 确保您正确地形成了auth标头(这几乎可以肯定,从您的问题中尚不清楚您是如何从邮递员复制它或尝试在C#中生成它的)

  • Ensure your credentials are valid. 确保您的凭据有效。

I've adjusted your code snippet to be explicit about correctly forming the header: 我已将您的代码段调整为明确有关正确形成标头的内容:

var base64Creds = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:password"));

var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "GET";

httpWebRequest.Headers.Add($"Authorization: Basic {base64Creds}");

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();

    Console.WriteLine(result);
}

If the above code snippet doesn't work with your main username and password, as a last resort you can try setting an alternate credential: ( https://dev.azure.com/ {organisation}/_usersSettings/altcreds) with a known username and password combination and see if that works for you. 如果以上代码段不适用于您的主要用户名和密码,则作为最后的选择,您可以尝试设置备用凭据:( https://dev.azure.com/ {organisation} / _ usersSettings / altcreds)用户名和密码组合,看看是否适合您。

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

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