在Azure Devops Rest API中授权 [英] Authorizing in azure devops rest API

查看:205
本文介绍了在Azure Devops Rest API中授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用azure devops api进行统计,但是发送标头时无法在c#中进行身份验证.

I have a use a azure devops api for statistics but i cant authenticate in c# when sending header.

我使用webrequests.

i use 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.

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);
            }

在C#中,我得到回应的都是一堆完整的html类型代码,我猜这是身份验证网站的一部分

in c# all im getting in response is whole bunch of html type code what i guess is part of authentication website

推荐答案

事实上,您会收到一堆HTML,这表明您没有以正确的方式形成基本的auth标头,或者您使用的用户名提供的内容未被识别.从代码示例中尚不清楚这是什么,但是如果您提供的格式正确的auth标头具有无效的凭据,则会收到401 Unauthorized响应,而HttpWebRequest类将作为异常抛出该响应.

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. 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标头(这几乎可以肯定,从问题如何从邮递员那里复制它还是尝试用C#生成它尚不清楚)

  • 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#)

确保您的凭据有效.

我已经对您的代码段进行了调整,以明确说明如何正确形成标头:

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.

这篇关于在Azure Devops Rest API中授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆