The .NET driver supports all MongoDB authentication mechanisms including those in the Enterprise Edition.

Authentication credentials are created by the application as instances of MongoCredential which includes static factory methods for each of the supported authentication mechanisms. A list of these instances must be passed to the driver using the MongoClient constructor that takes a MongoClientSettings. When only one credential is necessary, it is possible to specify via the connection string.


MongoDB 4.0 now uses SCRAM (Salted Challenge Response Authentication Mechanism) as the default mechanism and no longer supports MONGODB-CR. To create a credential that will authenticate properly regardless of server version, create a credential using the following static factory method.

var credential = MongoCredential.CreateCredential(databaseName, username, password);

Or via the connection string:


These are the recommended approaches as it will make upgrading from MongoDB 2.6 to MongoDB 3.0 seamless, before and after upgrading the authentication schema. For MongoDB 4.0, the above approaches will automatically determine which version of SCRAM should be used (SCRAM-SHA-1 or SCRAM-SHA-256).

The databaseName part of the connection string indicates which database the credentials are located in. See the connection string section for more information on connection strings.


When connecting to a MongoDB 4.0 server without specifying an authentication mechanism, the driver will negotiate with the server to determine whether SCRAM-SHA-1 or SCRAM-SHA-256 is the appropriate mechanism.


SCRAM-SHA-256 is the default authentication mechanism chosen as long as the user’s authentication mechanism supports it. (See the mechanism parameter of createUser() and the “Supported Authentication Methods: Defaults” section of the Driver Authentication Specification for additional information.


SecureStrings are slightly less secure when used in conjunction with SCRAM-SHA-256, due to the need to temporarily store the cleartext password in a managed memory string in order to SASLPrep it. This behavior is no different from other drivers in languages with managed memory. (SCRAM-SHA-1 is not affected.)

.NET Standard support

In .NET Standard, authenticating via SCRAM-SHA-256 may not work with non-ASCII passwords because SASLPrep is not fully implemented due to the lack of a string normalization function in .NET Standard 1.5. Normalizing the password into Unicode Normalization Form KC beforehand MAY help. SCRAM-SHA-1 is the recommended alternative for now. See RFC5802 and the SCRAM-SHA-256 section of the Driver Authentication specification for additional information.

x.509 Authentication

The x.509 mechanism authenticates a user whose name is derived from the distinguished subject name of the x.509 certificate presented by the driver during SSL negotiation. This authentication method requires the use of SSL connections with certificate validation and is available in MongoDB 2.6 and newer. To create a credential of this type, use the following static factory method:

var credential = MongoCredential.CreateX509Credential(username);

Or via the connection string:


Even when using the connection string to provide the credential, the certificate must still be provided via code. This certificate can be pulled out of the trust stores on the box, or from a file. However, to be used with client authentication, the X509Certificate provided to the driver must contain the PrivateKey.

var cert = new X509Certificate2("client.pfx", "mySuperSecretPassword");

var settings = new MongoClientSettings
    Credentials = new[] 
        MongoCredential.CreateMongoX509Credential("CN=client,OU=user,O=organization,L=Some City,ST=Some State,C=Some Country")
    SslSettings = new SslSettings
        ClientCertificates = new[] { cert },
    UseSsl = true


MongoDB Enterprise supports authentication using Kerberos/GSSAPI. To create a Kerberos/GSSAPI credential, use the following method:

var credential = MongoCredential.CreateGssapiCredential(username, password);

Or via the connection string:


Note that the username will need to have a REALM associated with it. When used in a connection string, %40 is the escape character for the @ symbol.

If the process owner running your application is the same as the user needing authentication, you can omit the password:

var credential = MongoCredential.CreateGssapiCredential(username);

Or via the connection string:


Depending on the kerberos setup, it may be required to specify some additional properties. These may be specified in the connection string or via code.


    Uses the DNS server to retrieve the fully qualified domain name (FQDN) of the host.

    credential = credential.WithMechanismProperty("CANONICALIZE_HOST_NAME", "true");

    Or via the connection string:


    This is used when the user’s realm is different from the service’s realm.

    credential = credential.WithMechanismProperty("REALM", "otherrealm");

    Or via the connection string:


    This is used when the service’s name is different that the default mongodb.

    credential = credential.WithMechanismProperty("SERVICE_NAME", "othername");

    Or via the connection string:


In addition, it is possible to use multiple authentication mechanism properties either via code or in the connection string. In code, call WithMechanismProperty multiple times. In the connection string, separate the entries with a , (comma).



MongoDB Enterprise supports proxy authentication through a Lightweight Directory Access Protocol (LDAP) service. To create a credential of type LDAP use the following static factory method:

var credential = MongoCredential.CreatePlainCredential("$external", username, password);

Or via the connection string:


Note that the method refers to the plain authentication mechanism instead of LDAP because technically the driver is authenticating via the PLAIN SASL mechanism. This means that your credentials are in plain text on the wire. Therefore, PLAIN should only be used in conjunction with SSL.