<p>I'm running PHP, Apache, and Windows.  I do not have a domain setup, so I would like my website's forms-based authentication to use the local user accounts database built in to Windows (I think it's called SAM).</p>

<p>I know that if Active Directory is setup, you can use the PHP LDAP module to connect and authenticate in your script, but without AD there is no LDAP.  What is the equivalent for standalone machines?</p>

[{'answer_id': 61062, 'author': 'Allain Lalonde', 'author_id': 2443, 'author_profile': 'https://Stackoverflow.com/users/2443', 'pm_score': 0, 'selected': False, 'text': "<p>Good Question!</p>\n\n<p>I've given this some thought... and I can't think of a good solution.  What I can think of is a horrible horrible hack that just might work.  After seeing that no one has posted an answer to this question for nearly a day, I figured a bad, but working answer would be ok. </p>\n\n<p>The SAM file is off limits while the system is running.  There are some DLL Injection tricks which you may be able to get working but in the end you'll just end up with password hashes and you'd have to hash the user provided passwords to match against them anyway.</p>\n\n<p>What you really want is something that tries to authenticate the user against the SAM file. I think you can do this by doing something like the following. </p>\n\n<ol>\n<li>Create a File Share on the server and make it so that only accounts that you want to be able to log in as are granted access to it.</li>\n<li>In PHP use the system command to invoke a wsh script that: mounts the share using the username and password that the website user provides. records if it works, and then unmounts the drive if it does. </li>\n<li>Collect the result somehow.  The result can be returned to php either on the stdout of the script, or hopefully using the return code for the script.</li>\n</ol>\n\n<p>I know it's not pretty, but it should work.</p>\n\n<p>I feel dirty :|</p>\n\n<p>Edit: reason for invoking the external wsh script is that PHP doesn't allow you to use UNC  paths (as far as I can remember).</p>\n"}, {'answer_id': 61220, 'author': 'VolkerK', 'author_id': 4833, 'author_profile': 'https://Stackoverflow.com/users/4833', 'pm_score': 3, 'selected': True, 'text': '<p>I haven\'t found a simple solution either. There are examples using CreateObject and the WinNT ADSI provider. But eventually they all bump into <a href="http://support.microsoft.com/kb/218497" rel="nofollow noreferrer">\nUser authentication issues with the Active Directory Service Interfaces WinNT provider</a>. I\'m not 100% sure but I <i>guess</i> the WSH/network connect approach has the same problem.<br />\nAccording to <a href="http://support.microsoft.com/kb/180548/" rel="nofollow noreferrer">How to validate user credentials on Microsoft operating systems</a> you should use LogonUser or SSPI.\n<br />\nIt also says<blockquote><pre>LogonUser Win32 API does not require TCB privilege in Microsoft Windows Server 2003, however, for downlevel compatibility, this is still the best approach.</p>\n\n<p>On Windows XP, it is no longer required that a process have the SE_TCB_NAME privilege in order to call LogonUser. Therefore, the simplest method to validate a user\'s credentials on Windows XP, is to call the LogonUser API.</pre></blockquote>Therefore, if I were certain Win9x/2000 support isn\'t needed, I would write an extension that exposes LogonUser to php.<br />\nYou might also be interested in  <a href="http://www.codewalkers.com/c/a/User-Management-Code/User-Authentication-from-NT-Accounts/" rel="nofollow noreferrer">User Authentication from NT Accounts</a>. It uses the w32api extension, and needs a support dll  ...I\'d rather write that small LogonUser-extension ;-)<br />\nIf that\'s not feasible I\'d probably look into the fastcgi module for IIS and how stable it is and let the IIS handle the authentication.</p>\n\n<p>edit:<br />\nI\'ve also tried to utilize <a href="http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.aspx" rel="nofollow noreferrer">System.Security.Principal.WindowsIdentity</a> and php\'s <a href="http://uk.php.net/manual/en/book.com.php" rel="nofollow noreferrer">com/.net extension</a>. But the dotnet constructor doesn\'t seem to allow passing parameters to the objects constructor and my "experiment" to get the assembly (and with it CreateInstance()) from GetType() has failed with an "unknown zval" error.</p>\n'}, {'answer_id': 66417, 'author': 'Martin', 'author_id': 2581, 'author_profile': 'https://Stackoverflow.com/users/2581', 'pm_score': 0, 'selected': False, 'text': '<p>Building a PHP extension requires a pretty large investment in terms of hard disk space.  Since I already have the GCC (MinGW) environment installed, I have decided to take the performance hit of launching a process from the PHP script.  The source code is below.</p>\n\n<pre><code>// Usage: logonuser.exe /user username /password password [/domain domain]\n// Exit code is 0 on logon success and 1 on failure.\n\n#include &lt;windows.h&gt;\n\nint main(int argc, char *argv[]) {\n    HANDLE r = 0;\n    char *user = 0;\n    char *password = 0;\n    char *domain = 0;\n    int i;\n\n    for(i = 1; i &lt; argc; i++) {\n        if(!strcmp(argv[i], "/user")) {\n            if(i + 1 &lt; argc) {\n                user = argv[i + 1];\n                i++;\n            }\n        } else if(!strcmp(argv[i], "/domain")) {\n            if(i + 1 &lt; argc) {\n                domain = argv[i + 1];\n                i++;\n            }\n        } else if(!strcmp(argv[i], "/password")) {\n            if(i + 1 &lt; argc) {\n                password = argv[i + 1];\n                i++;\n            }\n        }\n    }\n\n    if(user &amp;&amp; password) {\n        LogonUser(user, domain, password, LOGON32_LOGON_BATCH, LOGON32_PROVIDER_DEFAULT, &amp;r);\n    }\n    return r ? 0 : 1;\n}\n</code></pre>\n\n<p>Next is the PHP source demonstrating its use.\n    \n\n<pre><code>if($_SERVER[\'REQUEST_METHOD\'] == \'POST\') {\n    if(isset($_REQUEST[\'user\'], $_REQUEST[\'password\'], $_REQUEST[\'domain\'])) {\n        $failure = 1;\n        $user = $_REQUEST[\'user\'];\n        $password = $_REQUEST[\'password\'];\n        $domain = $_REQUEST[\'domain\'];\n\n        if($user &amp;&amp; $password) {\n            $cmd = "logonuser.exe /user " . escapeshellarg($user) . " /password " . escapeshellarg($password);\n            if($domain) $cmd .= " /domain " . escapeshellarg($domain);\n            system($cmd, $failure);\n        }\n\n        if($failure) {\n            echo("Incorrect credentials.");\n        } else {\n            echo("Correct credentials!");\n        }\n    }\n}\n?&gt;\n&lt;form action="&lt;?php echo(htmlentities($_SERVER[\'PHP_SELF\'])); ?&gt;" method="post"&gt;\n    Username: &lt;input type="text" name="user" value="&lt;?php echo(htmlentities($user)); ?&gt;" /&gt;&lt;br /&gt;\n    Password: &lt;input type="password" name="password" value="" /&gt;&lt;br /&gt;\n    Domain: &lt;input type="text" name="domain" value="&lt;?php echo(htmlentities($domain)); ?&gt;" /&gt;&lt;br /&gt;\n    &lt;input type="submit" value="logon" /&gt;\n&lt;/form&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/60585', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2581/']