# DirectoryConnection

> Binds the exact supplied credentials on a LdapRecord transport without dispatching authentication events.

[API reference](/docs/api/index.md) › [LDAP](/docs/extensions/ldap/reference/index.md) › DirectoryConnection

| class | `yii2\extensions\ldap\DirectoryConnection` |
| --- | --- |
| Extends | `LdapRecord\Ldap` |
| Source | [`src/DirectoryConnection.php`](https://github.com/yii2-extensions/ldap/blob/main/src/DirectoryConnection.php#L18) |

Binds the exact supplied credentials on a LdapRecord transport without dispatching authentication events.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`bind()`](#method-bind) | Binds a service account without transforming its password or allowing anonymous fallback. | `DirectoryConnection` |
| [`bindResultCode()`](#method-bindResultCode) | Returns the LDAP result code without interpreting account or server diagnostics. | `DirectoryConnection` |
| [`connect()`](#method-connect) | Opens a new handle after discarding the previous one, so its TLS state never carries over. | `DirectoryConnection` |
| [`setOption()`](#method-setOption) | Refuses to continue an operation if its requested server controls cannot be installed. | `DirectoryConnection` |

## Method details

### bind() <span id="method-bind"></span>

```php
public function bind(string|null $dn = null, string|null $password = null, array<array-key, mixed>|null $controls = null): LdapRecord\LdapResultResponse
```

Binds a service account without transforming its password or allowing anonymous fallback.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$dn` | `string\|null` | Service-account bind name. A missing or empty name is rejected. |
| `$password` | `string\|null` | Exact password supplied for the bind. A missing or empty password is rejected. |
| `$controls` | `array<array-key, mixed>\|null` | Optional LDAP request controls. |

**Returns**

`LdapRecord\LdapResultResponse`: Parsed bind response after validation.

**Throws**

- [`DirectoryUnavailableException`](/docs/extensions/ldap/reference/directoryunavailableexception/index.md): If the transport is not secure or the bind cannot be performed.

### bindResultCode() <span id="method-bindResultCode"></span>

```php
public function bindResultCode(string $principal, string $password): int
```

Returns the LDAP result code without interpreting account or server diagnostics.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$principal` | `string` | Directory bind name. |
| `$password` | `string` | Password to submit unchanged. |

**Returns**

`int`: LDAP result code; empty or NUL-containing credentials are treated as invalid credentials (49).

**Throws**

- [`DirectoryUnavailableException`](/docs/extensions/ldap/reference/directoryunavailableexception/index.md): when StartTLS has not completed, or the result cannot be obtained.

### connect() <span id="method-connect"></span>

```php
public function connect(array<array-key, mixed>|string $hosts = [], int $port = 389, string|null $protocol = null): bool
```

Opens a new handle after discarding the previous one, so its TLS state never carries over.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$hosts` | `array<array-key, mixed>\|string` | Directory host or hosts. |
| `$port` | `int` | Directory port. |
| `$protocol` | `string\|null` | URI scheme prefix, or `null` for the transport default. |

**Returns**

`bool`: Whether a handle was allocated.

### setOption() <span id="method-setOption"></span>

```php
public function setOption(int $option, mixed $value): bool
```

Refuses to continue an operation if its requested server controls cannot be installed.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$option` | `int` | LDAP option identifier. |
| `$value` | `mixed` | Value accepted by that LDAP option. |

**Returns**

`bool`: Whether the option was set.

**Throws**

- [`DirectoryUnavailableException`](/docs/extensions/ldap/reference/directoryunavailableexception/index.md): when the LDAP client rejects the server controls.