maor yadin
maor yadin

Reputation: 61

azure DevOps basic Auth using HttpClient (FAILED)

i am trying to Authenticate using HttpClient to my Azure Dev organization. but its always failed.

the only way to achieve success with authentication was using Client Library like this:

VssConnection connection = new VssConnection(new Uri(azureDevOpsOrganizationUrl), new VssClientCredentials());

hope someone can tell me what is it the proper way to auth using username and password only.

UPDATE: i also tried like this:

        string SecurelyStoredUserName = "EmailAddressAsUserName";
        SecureString SecurelyStoredPassword = new SecureString();

        string PWD = "MyVerySecuredPassword";
        PWD.ToList().ForEach(SecurelyStoredPassword.AppendChar);

        NetworkCredential myCred = new NetworkCredential(
        SecurelyStoredUserName, SecurelyStoredPassword, azureDevOpsOrganizationUrl);


        string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(SecurelyStoredUserName + ":" + SecurelyStoredPassword));

        HttpClientHandler handler;
        handler = new HttpClientHandler() { Credentials = myCred };
        HttpClient client;
        client = new HttpClient(handler);
        client.BaseAddress = new Uri(azureDevOpsOrganizationUrl);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Add("Accept", "application/json");
        client.DefaultRequestHeaders.Add("Authorization", "Basic " + svcCredentials);

that what i did, but when i tried to do a get/post , i get Error 401 Unauthorized

Upvotes: 2

Views: 1230

Answers (1)

jessehouwing
jessehouwing

Reputation: 115047

You can't send a network credential to Azure Devops. It doesn't accept that kind of authentication. You could use a Personal Access token, or use the Active Directory API to get access.

All is explained on the very first "Getting started" pages on how to use the Azure DevOps APIs.

A complete sample for Interactive User+Pass auth is available here.

If you're trying to act as a user on-behalf-of, then you may need to rethink your approach.

Upvotes: 3

Related Questions