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

标签 c# rest authentication teamcity-9.0

我最近一直在努力通过 API 在 TeamCity 中进行身份验证。我可以直接在我的浏览器中访问资源 ( http://usr:pw@teamcity:8111/httpAuth/app/rest/.. 。),但这样做会以编程方式返回 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

我可以毫无问题地使用 guestAuth( http://teamcity:8111/guestAuth/app/rest/projects ),因此 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);

关于c# - TeamCity - 无法通过 API 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31116793/

相关文章:

c# - Visual Studio 2005 缓慢关闭

c# - 使用Html.TextBoxFor时如何设置文本框的宽度

delphi - 在 Delphi 7 中的 TIdHTTPServer 中处理 PUT 和 DELETE 请求

node.js - 使用 Botframework 进行 OAuth 时的神奇代码。有办法吗?

Java:如何检查计算机是否已经被更改

c# - 如何使用 IDisposable 接口(interface)释放 excel 对象

c# - 如何在静态类中覆盖 ToString?

java - 以编程方式忽略(省略)REST 服务的 JSON 响应中的特定字段,而不更改 DTO 对象类

java - 客户端读取超时后 Jersey 服务器 CLOSE_WAIT 泄漏

authentication - 如何将密码加密到 key 表文件中?