【问题标题】:Establish connectivity between PHP Laravel and SQL Server - using Windows authentication在 PHP Laravel 和 SQL Server 之间建立连接 - 使用 Windows 身份验证
【发布时间】:2020-09-26 03:55:52
【问题描述】:

我有一个使用 PHP 7.2 和 SQL Server 2019 开发的应用程序。该应用程序托管在 IIS 10 上。我能够使用 db 的用户名和密码在应用程序和数据库之间建立连接。但是现在我的客户希望我们使用 Windows 身份验证来访问数据库。他们给了我们一个服务名和一个实例名,没有别的。

用于测试连接的代码是

<?php

$serverName = "DEV1ECLD001A\FEB19M"; //serverName\instanceName

$connectionInfo = array( "Database"=>"ECLIPSE");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
try {
  if( $conn ) {
      echo  "Connection established.<br />";
  }else{
     //throw new Exception('Unable to connect');
    echo "Connection could not be established.<br />";
       die( print_r( sqlsrv_errors(), true));
  }
}catch(Exception $e) {
  echo '{"error":{"text":'. $e->getMessage() .'}}';
}

?>

``````````

In php.ini, added the lines below

fastcgi.impersonate = 0
mssql.secure_connection = On

In IIS sever,

Anonymous Authentication = Disabled
Windows Authentication = Enabled

My DB and UI are in 2 different servers and I am able to ping both and access the DB if username and password are given but not through windows auth.

While trying to connect, get this error below:

Connection Could not be established. Login failed for user prod/DEV1ECLD001A$

Am I overlooking something ? Kindly help to resolve.

【问题讨论】:

  • 你有什么解决办法吗?

标签: php sql laravel windows authentication


【解决方案1】:

你需要:

  1. 服务帐户必须在 Active Directory 中,并且两个服务器也在同一个域(或认可的域)中
  2. 使用 PHP 的 SQLSrv 扩展 (PDO SQLSrv) 并安装 Microsoft ODBC Driver
  3. 从您的 PHP 代码中删除用户和密码信息。保持空白。
  4. 在 IIS 池应用程序高级配置中,定义服务帐户的身份(需要服务帐户的密码)
  5. 在 IIS 网站上,为匿名身份验证选中“使用池应用程序标识”并启用它。
  6. 在 SQL Server 上,在连接中添加来自 Active Directory 的服务帐户并允许访问数据库。
  7. 保持 PHP.INI 配置与“fastcgi.impersonate = 0”相同

注意,如果您使用 IIS Windows 身份验证,连接的用户需要对数据库的权限。

为了检查配置是否正确,IIS 启动的 PHP-CGI.exe 的 Windows 任务管理器中显示的帐户必须与应用程序池的身份配置中定义的帐户相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-11
    • 2011-04-10
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多