@@ -23,6 +23,7 @@ You can use the following authentication mechanisms with the latest version of
2323
2424- :ref:`GSSAPI/Kerberos <csharp-kerberos>`
2525- :ref:`LDAP (Plain) <csharp-LDAP>`
26+ - :ref:`MONGODB-OIDC <csharp-mongodb-oidc>`
2627
2728To authenticate using another mechanism, see the
2829:ref:`<csharp-authentication-mechanisms>` fundamentals page. For
@@ -255,6 +256,127 @@ mechanism:
255256 authenticates using the PLAIN Simple Authentication and Security Layer
256257 (SASL) defined in `RFC-4616 <https://tools.ietf.org/html/rfc4616>`__.
257258
259+ .. _csharp-mongodb-oidc:
260+
261+ MONGODB-OIDC
262+ ------------
263+
264+ .. important::
265+
266+ The MONGODB-OIDC authentication mechanism requires MongoDB v7.0 or later running
267+ on a Linux platform.
268+
269+ The following sections describe how to use the MONGODB-OIDC authentication mechanism to
270+ authenticate from various platforms.
271+
272+ For more information about the MONGODB-OIDC authentication mechanism, see
273+ :manual:`OpenID Connect Authentication </core/security-oidc/>` and
274+ :manual:`MongoDB Server Parameters </reference/parameters/#mongodb-parameter-param.oidcIdentityProviders>`
275+ in the MongoDB Server manual.
276+
277+ .. _csharp-mongodb-oidc-azure-imds:
278+
279+ Azure IMDS
280+ ~~~~~~~~~~
281+
282+ If your application runs on an Azure VM, or otherwise uses the
283+ `Azure Instance Metadata Service <https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service>`__
284+ (IMDS), you can authenticate to MongoDB by using the {+driver-short+}'s built-in Azure
285+ support.
286+
287+ You can specify Azure IMDS OIDC authentication on a ``MongoClientSettings`` object either by
288+ using a ``MongoCredential`` object or as part of the connection string. Select the
289+ :guilabel:`Connection String` or :guilabel:`MongoCredential` tab to
290+ see the corresponding syntax.
291+
292+ .. tabs::
293+
294+ .. tab:: Connection String
295+ :tabid: mongodb-azure-imds-connection-string
296+
297+ The following code example shows how to specify Azure IMDS OIDC authentication.
298+ Replace the ``<percent-encoded audience>`` placeholder with the percent-encoded
299+ value of the ``audience`` parameter configured on your MongoDB deployment.
300+
301+ .. code-block:: csharp
302+
303+ var connectionString = "mongodb://<username>@<hostname>[:<port>]/?" +
304+ "authMechanism=MONGODB-OIDC" +
305+ "&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>");
306+ var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
307+ var client = new MongoClient(mongoClientSettings);
308+
309+ .. tab:: MongoCredential
310+ :tabid: mongodb-azure-mongo-credential
311+
312+ The following code example shows how to specify Azure IMDS OIDC authentication.
313+ Replace the ``<username>`` placeholder with the client ID or application ID of the
314+ Azure managed identity or enterprise application. Replace the ``<audience>``
315+ placeholder with the value of the ``audience`` parameter configured on your MongoDB
316+ deployment.
317+
318+ .. code-block:: csharp
319+
320+ var mongoClientSettings = MongoClientSettings.FromConnectionString(
321+ "mongodb+srv://<hostname>[:<port>]");
322+ mongoClientSettings.Credential = MongoCredential.CreateOidcCredential("azure", "<username>")
323+ .WithMechanismProperty("TOKEN_RESOURCE", "<audience>");
324+ var client = new MongoClient(mongoClientSettings);
325+
326+ Custom Callback
327+ ~~~~~~~~~~~~~~~
328+
329+ The {+driver-short+} doesn't offer built-in support for all platforms, including
330+ Azure Functions and Azure Kubernetes Service (AKS). Instead, you
331+ must define a custom callback to use OIDC to authenticate from these platforms.
332+
333+ First, define a class that implements the ``IOidcCallback`` interface. This interface
334+ contains two methods:
335+
336+ - ``GetOidcAccessToken()``: This method accepts the parameters to the callback method
337+ and returns the callback response.
338+ - ``GetOidcAccessTokenAsync()``: This method is an asynchronous version of the previous
339+ method.
340+
341+ The following code is an example implementation of the ``IOidcCallback`` interface.
342+ In this example, the methods retrieve an OIDC token from a file named ``"access-token.dat"``
343+ in the local file system.
344+
345+ .. code-block:: csharp
346+
347+ public class MyCallback : IOidcCallback
348+ {
349+ public OidcAccessToken GetOidcAccessToken(
350+ OidcCallbackParameters parameters,
351+ CancellationToken cancellationToken)
352+ {
353+ var accessToken = File.ReadAllText("access-token.dat");
354+ return new(accessToken, expiresIn: null);
355+ }
356+
357+ public async Task<OidcAccessToken> GetOidcAccessTokenAsync(
358+ OidcCallbackParameters parameters,
359+ CancellationToken cancellationToken)
360+ {
361+ var accessToken = await File.ReadAllTextAsync(
362+ "access-token.dat",
363+ cancellationToken)
364+ .ConfigureAwait(false);
365+ return new(accessToken, expiresIn: null);
366+ }
367+ }
368+
369+ After you define a class that contains your custom callback methods, call the
370+ ``MongoCredential.CreateOidcCredential()`` method and pass in a new instance of your
371+ class. Store the result of this method call in the ``Credential`` property of your
372+ ``MongoClientSettings`` object, as shown in the following code example:
373+
374+ .. code-block:: csharp
375+
376+ var mongoClientSettings = MongoClientSettings.FromConnectionString("mongodb://<hostname>[:port]");
377+ mongoClientSettings.Credential = MongoCredential.CreateOidcCredential(new MyCallback());
378+ var client = new MongoClient(mongoClientSettings);
379+
258380API Documentation
259381-----------------
260382
@@ -266,3 +388,4 @@ guide, see the following API Documentation:
266388- `MongoClientSettings <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoClientSettings.html>`__
267389- `CreateGssapiCredential() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoCredential.CreateGssapiCredential.html>`__
268390- `CreatePlainCredential() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoCredential.CreatePlainCredential.html>`__
391+ - `IOidcCallback <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Authentication.Oidc.IOidcCallback.html>`__
0 commit comments