【问题标题】:How can I set the NTLM credential (as in my .NET controller) into a CURL request?如何将 NTLM 凭据(如在我的 .NET 控制器中)设置为 CURL 请求?
【发布时间】:2018-11-21 14:47:21
【问题描述】:

我正在开发一个 .NET 项目。在这个项目的控制器中,我正在调用一个指定身份验证的外部 API,以这种方式:

private NetworkCredential myCreds = new NetworkCredential("MYUSERNAME", "MYPASSWORD", "MYDOMAIN");

private CredentialCache = new CredentialCache();

string jsonRequest = urlBaseProtocolloApi + "/api/MY_ENDPOINT";

credCache.Add(new Uri(jsonRequest), "NTLM", myCreds);

HttpWebRequest spRequest = (HttpWebRequest)HttpWebRequest.Create(jsonRequest);
spRequest.Credentials = credCache;
spRequest.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
spRequest.Method = "GET";
spRequest.Accept = "application/json;odata=verbose";

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

HttpWebResponse endpointResponse = (HttpWebResponse)spRequest.GetResponse();

效果很好。

如您所见,我正在使用此 NTLM 协议对调用的 API 执行身份验证。

我的问题是,出于测试原因,我想使用 curl 执行此调用,而不是从我的 .NET 控制器传递。

我是这样尝试的:

curl -X POST -k -d @invio_a_protocollo.json https://my_machine:13003/API_CONTEXT/api/MY_ENDPOINT --header "Content-Type:application/json

但显然,由于我没有传递凭据,因此我收到了以下错误消息:

{"Message":"Authorization has been denied for this request."}

如何尝试在我的 curl 请求中设置此 NTLM

【问题讨论】:

    标签: .net curl credentials ntlm


    【解决方案1】:

    您的 .net 会自动使用 kerberos 或 ntlm(又名 WIA)。在 curl 中,您必须使用 --ntlm--negotiate--anyauth--user 标志。

    一些例子:

    这将尝试 ntlm: curl -X POST -k --ntlm --user domain\user:password -d @invio_a_protocollo.json https://my_machine:13003/API_CONTEXT/api/MY_ENDPOINT --header "Content-Type:application/json

    这将尝试协商: curl -X POST -k --negotiate --user user:password -d @invio_a_protocollo.json https://my_machine:13003/API_CONTEXT/api/MY_ENDPOINT --header "Content-Type:application/json

    这将根据 IIS 设置尝试 kerberos 或 ntlm: curl -X POST -k --anyauth --user user:password -d @invio_a_protocollo.json https://my_machine:13003/API_CONTEXT/api/MY_ENDPOINT --header "Content-Type:application/json

    已知错误: curl 中有两个与 ntlm HTTPS 和 POST 请求相关的已知错误:

    1. Curl 丢弃带有 ntlm 的 POST 请求的负载: https://github.com/curl/curl/issues/2431
    2. 当 IIS 的“扩展保护”为 Required 时,NTLM 返回 401:https://github.com/curl/curl/issues/3280。要解决此问题,请尽可能关闭扩展保护并使用安全特性 (https://docs.microsoft.com/en-us/iis/configuration/system.webserver/security/authentication/windowsauthentication/extendedprotection/#how-to)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 2014-02-18
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      相关资源
      最近更新 更多