C# TeamCity-无法通过API进行身份验证

C# TeamCity-无法通过API进行身份验证,c#,rest,authentication,teamcity-9.0,C#,Rest,Authentication,Teamcity 9.0,我最近一直在努力通过API在TeamCity进行身份验证。我可以直接在浏览器中访问资源(.),但以编程方式访问会返回401个未经授权的资源 WebRequest request = WebRequest.Create("http://user:pwd@teamcity:8111/httpAuth/app/rest/projects"); request.Method = WebRequestMethods.Http.Get; try {

我最近一直在努力通过API在TeamCity进行身份验证。我可以直接在浏览器中访问资源(.),但以编程方式访问会返回401个未经授权的资源

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
我可以毫无问题地使用guestAuth(),因此WebRequest本身应该没有任何问题


有人有主意吗

尝试添加您的凭据,然后提出请求。这将得到您需要的

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

。您应该发送客户端凭据。然后只有响应成功。我在这里看到了一个示例,我认为已经足够了。(@…此curl请求请看此示例。。特别是“CreateHttpClient”非常感谢!根据这里的另一个答案创建了请求,但我想我是在wront上下文中使用它的。。