简体   繁体   English

使用cURL的PHP​​ NTLM会话

[英]PHP NTLM session with cURL

So a little trivia first.. 所以先来一点琐事。

There is written in ASP.NET website, which uses NTLM protocol to authenticate users that want to log in. It's perfectly ok when they normally use it, they type in website URL, they provide their credentials, authenticate and maintain session in web browser. 有一个用ASP.NET网站编写的网站,该网站使用NTLM协议对要登录的用户进行身份验证。通常情况下,用户可以键入网站URL,提供凭据,在网络浏览器中进行身份验证和维护会话,这完全可以。

What I want to do, is create PHP website that will act as bot. 我想做的是创建将充当机器人的PHP网站。 It is my companys internal website and I am approved to do so. 这是我公司的内部网站,我已被批准这样做。 The problem I run into, is managing session. 我遇到的问题是管理会话。 Users will be able to type in their credentials in my PHP website, and my PHP website will authenticate them to target site, using cURL. 用户将能够在我的PHP网站中输入其凭据,并且我的PHP网站将使用cURL将其身份验证到目标站点。

The code I got so far is: 到目前为止,我得到的代码是:

    $cookie_file_path = dirname(__FILE__) . '/cookies.txt';
    $ch = curl_init();

    //==============================================================
     curl_setopt($ch, CURLOPT_USERPWD, $username. ':' . $password);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLINFO_HEADER_OUT, true);
     curl_setopt($ch, CURLOPT_FAILONERROR, 0);
     curl_setopt($ch, CURLOPT_MAXREDIRS, 100);
    //=============================================================
    $ret = curl_exec($ch);

Above code logs in to target website by cURL (which manages NTLM handshake, as it seems), and fetches websites content. 上面的代码通过cURL(似乎管理NTLM握手)登录到目标网站,并获取网站内容。 It also stores Session ID that is sent back in cookie file. 它还存储发送回cookie文件中的会话ID。

What I'm trying to do next, is comment the CURLOPT_USERPWD option, in hope that this script will use session ID stored in cookie file to authenticate previously logged in user in second execution of this script. 接下来,我要注释CURLOPT_USERPWD选项,以期该脚本将使用存储在cookie文件中的会话ID来验证该脚本的第二次执行中先前登录的用户。 It could get rid of user credentials and do not store it anywhere that way, becouse it is not safe to store it in manually created session, database, or anywhere else. 它可能会摆脱用户凭证,并且不会以这种方式存储在用户凭证中,因为将其存储在手动创建的会话,数据库或其他任何地方都不安全。

I need this becouse bot will be using CRON to periodically check if website status has changed and perform some user actions as reaction to this. 我需要这个机器人,因为该机器人将使用CRON定期检查网站状态是否已更改,并执行一些用户操作以对此做出反应。 But to do this, user first must be authenticated, and his username and password must not be stored anywhere, so I have to use session information estabilished when he initially logged in. 但是要做到这一点,首先必须对用户进行身份验证,并且不能将用户名和密码存储在任何地方,因此我必须使用最初登录时已建立的会话信息。

CURL seems to NOT DO THIS. CURL似乎不这样做。 When I execute script second time with commented CURLOPT_USERPWD option, it does not use stored cookie to keep beeing authenticated. 当我第二次使用带注释的CURLOPT_USERPWD选项执行脚本时,它不使用存储的cookie来保持对beeing的身份验证。 Instead, it REWRITES cookie file with not relevant data send to me from service as response to NOT AUTHRORISED access request. 相反,它会将不相关数据的Cookie文件从服务发送给我,以响应未授权的访问请求。

My questions are: Why cURL doesnt use stored session information to keep beeing authenticated? 我的问题是:为什么cURL不使用存储的会话信息来保持bee身份验证? Is there any way to maintain this session with cURL and NTLM protocol based website? 有什么方法可以使用基于cURL和NTLM协议的网站维护此会话?

Thanks in advance. 提前致谢。

A few Month ago I had a similar problem then you. 几个月前,我也遇到了类似的问题。 I tried to get a connection to a navision soap api. 我试图与navision soap api建立连接。 Navision use the ntlm authentication. Navision使用ntlm身份验证。 The problem is that curl doesn't native support ntlm so you have to do it yourself. 问题是curl不本地支持ntlm,因此您必须自己做。

A blog post that helped me a lot in this situation was the following: 在这种情况下,对我有很大帮助的博客文章如下:

http://rabaix.net/en/articles/2008/03/13/using-soap-php-with-ntlm-authentication http://rabaix.net/zh/articles/2008/03/13/using-soap-php-with-ntlm-authentication

** Edit **编辑

Sorry i misread you question. 对不起,我没看懂你的问题。 You problem is simple. 您的问题很简单。

Just receive the header from a request with this line 只需从带有此行的请求中接收标头

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

You can then get from the result of curl_exec function, the Set-Cookie header. 然后,您可以从curl_exec函数的结果(Set-Cookie标头)中获取。

preg_match('/^Set-Cookie:\s*([^;]*)/mi', $ret, $match);
$cookie = parse_url($match[0]);

Now you can store it somewhere, and use it on the 2ten request. 现在您可以将其存储在某个位置,并在2ten请求中使用它。

I have the same problem and i solved it using curl_setopt($ch, CURLOPT_COOKIEFILE, ""); 我有同样的问题,我使用curl_setopt($ch, CURLOPT_COOKIEFILE, ""); line of code. 代码行。 The string should be exactly empty . 该字符串应完全为

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

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