C# 到 Teamcity NTLM 身份验证失败

C# to Teamcity NTLM Authentication failing

我无法使用 NTLM 从 C# 向 Teamcity 进行身份验证。它适用于浏览器和 Postman。

启用日志记录后,它似乎进行了 NTLM 握手,但随后出现 401 错误:

The token supplied to the function is invalid
To login manually go to "/login.html" page       

示例代码如下。我不确定这里出了什么问题。它适用于基本身份验证和修改后的 URI,包括 httpAuth。

        string uri = "http://teamcityserver/ntlmAuth/action.html?add2Queue=SomeBuild";

        CredentialCache cc = new CredentialCache();
        cc.Add(new Uri(uri), "NTLM", new NetworkCredential("user", "password")); // Have also tried default credentials

        var req = HttpWebRequest.Create(uri);
        req.Method = "POST";
        req.Credentials = cc;
        req.Headers.Add("Origin: http://teamcity");

知道了!缺少的因素是在 NTLM 来回期间客户端没有发送 cookie。

添加此修复:

req.CookieContainer = new CookieContainer();