【问题标题】:TeamCity - Unable to authenticate via APITeamCity - 无法通过 API 进行身份验证
【发布时间】:2015-09-15 23:34:12
【问题描述】:

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

有人有想法吗?

【问题讨论】:

标签: c# rest authentication teamcity-9.0


【解决方案1】:

尝试添加您的凭据,然后发出请求。它会得到您需要的。

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

【讨论】:

  • 非常感谢!根据此处的另一个答案创建请求,但我想我在错误的上下文中使用它..
猜你喜欢
  • 1970-01-01
  • 2012-04-01
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多