# WithKeyFile

- Module: [Aspire.Hosting.MongoDB](/reference/api/go/aspire.hosting.mongodb.md)
- Version: `17.0.0`
- Kind: `method`
- Defined on: [`MongoDBServerResource`](/reference/api/go/aspire.hosting.mongodb/mongodbserverresource.md)
- Source: [GitHub](https://github.com/microsoft/aspire/tree/bcfa151cdca6d0a573f22d5d9c23f9c727cb3b2f)

Sets up a keyfile for internal authentication between members of a MongoDB replica set, with the specified `keyValue` as the content of the file.

## Remarks

The keyfile is a shared secret. Every member of the replica set (or sharded cluster) should have the same keyfile, and possession of that secret is what authenticates a connection as "a legitimate member of this cluster."
See https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set-with-keyfile-access-control/

## Definition

```go
func (MongoDBServerResource) WithKeyFile(
    keyValue ExpressionValue,
    options ...*WithKeyFileOptions,
) MongoDBServerResource
```

## Parameters

- `keyValue` (`ExpressionValue`) -- The content of the keyfile. This is a shared secret: every member of the replica set has to be given the same value, and anything holding it can authenticate as a member of the cluster.
- `options` (`...*WithKeyFileOptions`) -- optional settings, see [Options](#options)

## Options

Optional parameters are passed in a trailing `options ...*WithKeyFileOptions` argument.

- `KeyFilePath` (`*string`) -- The absolute path the keyfile is mounted at inside the container, which has to include a file name. Defaults to `/etc/rs.key`.

## Returns

[`MongoDBServerResource`](/reference/api/go/aspire.hosting.mongodb/mongodbserverresource.md) -- The resource builder.

Returns a handle; failures surface later via `Err()`.

## Applies to

- [`MongoDBServerResource`](/reference/api/go/aspire.hosting.mongodb/mongodbserverresource.md)
