Search code examples
c#restauthenticationteamcity-9.0

TeamCity - Unable to authenticate via API


I've been struggling with authentication in TeamCity through the API lately. I can access the resources directly in my browser (http://usr:pw@teamcity:8111/httpAuth/app/rest/...), but doing so programmatically returns 401-Unauthorized.

WebRequest request = WebRequest.Create("http://user:pwd@teamcity:8111/httpAuth/app/rest/projects");
        request.Method = WebRequestMethods.Http.Get;
        try
        {
            request.Timeout = Timeout.Infinite;
             WebResponse response = request.GetResponse(); //Returns 401:Unauthorized

I can use guestAuth(http://teamcity:8111/guestAuth/app/rest/projects) without any problem, so there should not be any problem with the WebRequest itself.

Does anyone have an idea?


Solution

  • Try to add your credentials and then make request.it will be get what you need.

        var username = "abc";
        var password = "123";
        var encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
        request.Headers.Add("Authorization", "Basic " + encoded);