简体   繁体   English

C#到Teamcity NTLM身份验证失败

[英]C# to Teamcity NTLM Authentication failing

I'm unable to authenticate with Teamcity from C# with NTLM. 我无法通过NTLM从C#向Teamcity进行身份验证。 It works in the browser and with Postman. 它可以在浏览器中和Postman中使用。

With logging turned on it seems to do the NTLM handshake, but then settles on a 401 error with: 启用日志记录后,它似乎进行了NTLM握手,但是随后出现了以下401错误:

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

Sample code is below. 示例代码如下。 I'm not sure what's wrong here. 我不确定这是怎么回事。 It works with Basic authentication and a modified URI including httpAuth. 它适用于基本身份验证和包含httpAuth的修改后的URI。

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

Got it! 得到它了! The missing factor was that the cookies were not being sent by the client during the NTLM back-and-forth. 缺少的因素是在往返NTLM期间客户端未发送cookie。

Adding this fixed it: 添加此修复程序:

req.CookieContainer = new CookieContainer();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM