简体   繁体   English

您可以使用PHP获得Windows(AD)用户名吗?

[英]Can you get a Windows (AD) username in PHP?

I have a PHP web application on an intranet that can extract the IP and host name of the current user on that page, but I was wondering if there is a way to get/extract their Active Directory/Windows username as well. 我在Intranet上有一个PHP Web应用程序,可以在该页面上提取当前用户的IP和主机名,但是我想知道是否还有一种方法来获取/提取其Active Directory / Windows用户名。 Is this possible? 这可能吗?

Check the AUTH_USER request variable. 检查AUTH_USER请求变量。 This will be empty if your web app allows anonymous access, but if your server's using basic or Windows integrated authentication, it will contain the username of the authenticated user. 如果您的Web应用程序允许匿名访问,则此字段为空,但是如果您的服务器使用的是基本身份验证或Windows集成身份验证,则它将包含已验证用户的用户名。

In an Active Directory domain, if your clients are running Internet Explorer and your web server/filesystem permissions are configured properly, IE will silently submit their domain credentials to your server and AUTH_USER will be MYDOMAIN\\user.name without the users having to explicitly log in to your web app. 在Active Directory域中,如果您的客户端运行的是Internet Explorer,并且您的Web服务器/文件系统权限配置正确,则IE会以静默方式将其域凭据提交到您的服务器,而AUTH_USER将为MYDOMAIN\\user.name而无需用户明确登录进入您的网络应用。

I've got php mysql running on IIS - I can use $_SERVER["AUTH_USER"] if I turn on Windows Authentication in IIS -> Authentication and turn off Anonymous authentication (important) 我已经在IIS上运行php $_SERVER["AUTH_USER"] 如果在IIS中打开Windows身份验证->身份验证,然后关闭匿名身份验证(重要),则可以使用$_SERVER["AUTH_USER"]

I've used this to get my user and domain: 我用它来获取用户和域:

$user = $_SERVER['AUTH_USER'];

$user will return a value like: DOMAIN\\username on our network, and then it's just a case of removing the DOMAIN\\ from the string. $user将在我们的网络上返回一个类似DOMAIN\\username的值,然后只是从字符串中删除DOMAIN\\的情况。

This has worked in IE, FF, Chrome, Safari (tested) so far. 到目前为止,这已在IE,FF,Chrome,Safari(已测试)中运行。

Look at the PHP LDAP library functions: http://us.php.net/ldap . 查看PHP LDAP库函数: http : //us.php.net/ldap

Active Directory [mostly] conforms to the LDAP standard. Active Directory [大部分]符合LDAP标准。

If you're using Apache on Windows, you can install the mod_auth_sspi from 如果您在Windows上使用Apache,则可以从以下位置安装mod_auth_sspi:

https://sourceforge.net/projects/mod-auth-sspi/ https://sourceforge.net/projects/mod-auth-sspi/

Instructions are in the INSTALL file, and there is a whoami.php example. 说明在INSTALL文件中,并且有一个whoami.php示例。 (It's just a case of copying the mod_auth_sspi.so file into a folder and adding a line into httpd.conf.) (这只是将mod_auth_sspi.so文件复制到文件夹并将一行添加到httpd.conf中的情况。)

Once it's installed and the necessary settings are made in httpd.conf to protect the directories you wish, PHP will populate the $_SERVER['REMOTE_USER'] with the user and domain ('USER\\DOMAIN') of the authenticated user in IE -- or prompt and authenticate in Firefox before passing it in. 安装完毕并在httpd.conf中进行了必要的设置以保护您想要的目录后,PHP将使用IE中经过身份验证的用户的用户和域(“ USER \\ DOMAIN”)填充$_SERVER['REMOTE_USER'] - -或在传入之前在Firefox中提示并进行身份验证。

Info is session-based, so single(ish) signon is possible even in Firefox... 信息是基于会话的,因此即使在Firefox中也可以进行单点登录。

-Craig -克雷格

我们的环境中有多个域,因此我将pr​​eg_replace与regex结合使用,仅获得不带DOMAIN \\的用户名。

preg_replace("/^.+\\\\/", "", $_SERVER["AUTH_USER"]);

If you are looking for retrieving remote user IDSID/Username, use: 如果您要检索远程用户IDSID /用户名,请使用:

echo gethostbyaddr($_SERVER['REMOTE_ADDR']);

You will get something like iamuser1-mys.corp.company.com 您将获得类似iamuser1-mys.corp.company.com的信息

Filter the rest of the domain behind, and you are able to get the idsid only. 过滤其余的域,您将只能获得idsid。

For more information visit http://lostwithin.net/how-to-get-users-ip-and-computer-name-using-php/ 有关更多信息,请访问http://lostwithin.net/how-to-get-users-ip-and-computer-name-using-php/

You could probably authenticate the user in Apache with mod_auth_kerb by requiring authenticated access to some files … I think that way, the username should also be available in PHP environment variables somewhere … probably best to check with <?php phpinfo(); ?> 您可能可以通过要求对某些文件的身份验证访问来使用mod_auth_kerb 在Apache中 用户进行身份验证……我想这样,用户名也应该在某处的PHP环境变量中可用……可能最好用<?php phpinfo(); ?>进行检查<?php phpinfo(); ?> <?php phpinfo(); ?> once you get it runnning. <?php phpinfo(); ?>一旦获得运行。

Check out patched NTLM authentication module for Apache https://github.com/rsim/mod_ntlm 检出Apache https://github.com/rsim/mod_ntlm修补的NTLM身份验证模块

Based on NTLM auth module for Apache/Unix http://modntlm.sourceforge.net/ 基于适用于Apache / Unix的NTLM身份验证模块http://modntlm.sourceforge.net/

Read more at http://blog.rayapps.com/ http://blog.rayapps.com/上了解更多信息。

Source: http://imthi.com/blog/programming/leopard-apache2-ntlm-php-integrated-windows-authentication.php 资料来源: http : //imthi.com/blog/programming/leopard-apache2-ntlm-php-integrated-windows-authentication.php

get_user_name works the same way as getenv('USERNAME'); get_user_name与getenv('USERNAME')的工作方式相同;

I had encoding(with cyrillic) problems using getenv('USERNAME') 我使用getenv('USERNAME')编码(西里尔字母)问题

No. But what you can do is have your Active Directory admin enable LDAP so that users can maintain one set of credentials 否。但是您可以做的是让您的Active Directory管理员启用LDAP,以便用户可以维护一组凭据

http://us2.php.net/ldap http://us2.php.net/ldap

您可以说getenv('USERNAME')

Referencing trying to also figure out if AUTH_USER is part of a particular domain group; 引用尝试还找出AUTH_USER是否属于特定域组; a clever way to do this is t create a locked down folder with text files (can be blank). 一个聪明的方法是用文本文件创建一个可锁定的文件夹(可以为空白)。 Set security to only having the security/distro group you want to validate. 将安全性设置为仅具有要验证的安全性/发行版组。 Once you run a @file_get_contents (<---will toss a warning)...if the user does not have group access they will not be able to get the file contents and hence, will not have that particular AD group access. 一旦运行@file_get_contents(<---会发出警告)...如果用户没有组访问权限,他们将无法获取文件内容,因此将没有该特定的AD组访问权限。 This is simple and works wonderfully. 这很简单,效果很好。

This is a simple NTLM AD integration example, allows single sign on with Internet Explorer, requires login/configuration in other browsers. 这是一个简单的NTLM AD集成示例,允许使用Internet Explorer进行单点登录,需要在其他浏览器中进行登录/配置。

PHP Example PHP示例

<?php 
$user = $_SERVER['REMOTE_USER'];
$domain = getenv('USERDOMAIN');

?>

In your apache httpd.conf file 在您的apache httpd.conf文件中

LoadModule authnz_sspi_module modules/mod_authnz_sspi.so

<Directory "/path/to/folder">
    AllowOverride     All
    Options           ExecCGI
    AuthName          "SSPI Authentication"
    AuthType          SSPI
    SSPIAuth          On
    SSPIAuthoritative On
    SSPIOmitDomain    On
    Require           valid-user
    Require           user "NT AUTHORITY\ANONYMOUS LOGON" denied
</Directory>

And if you need the module, this link is useful: 如果您需要该模块,则此链接很有用:

https://www.apachehaus.net/modules/mod_authnz_sspi/ https://www.apachehaus.net/modules/mod_authnz_sspi/

使用此代码:

shell_exec("wmic computersystem get username")

try this code : 试试这个代码:

$user= shell_exec("echo %username%"); 
    echo "user : $user";

you get your windows(AD) username in php 您在php中获得Windows(AD)用户名

I tried almost all of these suggestions, but they were all returning empty values. 我尝试了几乎所有这些建议,但是它们都返回了空值。 If anyone else has this issue, I found this handy function on php.net ( http://php.net/manual/en/function.get-current-user.php ): 如果还有其他人遇到此问题,我在php.net( http://php.net/manual/en/function.get-current-user.php )上找到了这个方便的功能:

get_current_user();
$username = get_current_user();
echo $username;

This was the only way I was finally able to get the user's active directory username. 这是我最终能够获得用户的活动目录用户名的唯一方法。 If none of the above answers has worked, give this a try. 如果以上答案均无效,请尝试一下。

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

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