Table of Contents

Class MongoCredential

Namespace
MongoDB.Driver
Assembly
MongoDB.Driver.dll

Credential to access a MongoDB database.

[Serializable]
public class MongoCredential : IEquatable<MongoCredential>
Inheritance
MongoCredential
Implements
Inherited Members

Constructors

MongoCredential(string, MongoIdentity, MongoIdentityEvidence)

Initializes a new instance of the MongoCredential class.

public MongoCredential(string mechanism, MongoIdentity identity, MongoIdentityEvidence evidence)

Parameters

mechanism string

Mechanism to authenticate with.

identity MongoIdentity

The identity.

evidence MongoIdentityEvidence

The evidence.

Properties

Evidence

Gets the evidence.

public MongoIdentityEvidence Evidence { get; }

Property Value

MongoIdentityEvidence

Identity

Gets the identity.

public MongoIdentity Identity { get; }

Property Value

MongoIdentity

Mechanism

Gets the mechanism to authenticate with.

public string Mechanism { get; }

Property Value

string

Password

Gets the password.

[Obsolete("Use Evidence instead.")]
public string Password { get; }

Property Value

string

Source

Gets the source.

public string Source { get; }

Property Value

string

Username

Gets the username.

public string Username { get; }

Property Value

string

Methods

CreateCredential(string, string, SecureString)

Creates a default credential. Less secure when used in conjunction with SCRAM-SHA-256, due to the need to store the password in a managed string in order to SaslPrep it.

public static MongoCredential CreateCredential(string databaseName, string username, SecureString password)

Parameters

databaseName string

Name of the database.

username string

The username.

password SecureString

The password.

Returns

MongoCredential

A default credential.

CreateCredential(string, string, string)

Creates a default credential.

public static MongoCredential CreateCredential(string databaseName, string username, string password)

Parameters

databaseName string

Name of the database.

username string

The username.

password string

The password.

Returns

MongoCredential

A default credential.

CreateGssapiCredential(string)

Creates a GSSAPI credential.

public static MongoCredential CreateGssapiCredential(string username)

Parameters

username string

The username.

Returns

MongoCredential

A credential for GSSAPI.

Remarks

This overload is used primarily on linux.

CreateGssapiCredential(string, SecureString)

Creates a GSSAPI credential.

public static MongoCredential CreateGssapiCredential(string username, SecureString password)

Parameters

username string

The username.

password SecureString

The password.

Returns

MongoCredential

A credential for GSSAPI.

CreateGssapiCredential(string, string)

Creates a GSSAPI credential.

public static MongoCredential CreateGssapiCredential(string username, string password)

Parameters

username string

The username.

password string

The password.

Returns

MongoCredential

A credential for GSSAPI.

CreateMongoCRCredential(string, string, SecureString)

Creates a credential used with MONGODB-CR.

[Obsolete("MONGODB-CR was replaced by SCRAM-SHA-1 in MongoDB 3.0, and is now deprecated.")]
public static MongoCredential CreateMongoCRCredential(string databaseName, string username, SecureString password)

Parameters

databaseName string

Name of the database.

username string

The username.

password SecureString

The password.

Returns

MongoCredential

A credential for MONGODB-CR.

CreateMongoCRCredential(string, string, string)

Creates a credential used with MONGODB-CR.

[Obsolete("MONGODB-CR was replaced by SCRAM-SHA-1 in MongoDB 3.0, and is now deprecated.")]
public static MongoCredential CreateMongoCRCredential(string databaseName, string username, string password)

Parameters

databaseName string

Name of the database.

username string

The username.

password string

The password.

Returns

MongoCredential

A credential for MONGODB-CR.

CreateMongoX509Credential(string)

Creates a credential used with MONGODB-X509.

public static MongoCredential CreateMongoX509Credential(string username = null)

Parameters

username string

The username.

Returns

MongoCredential

A credential for MONGODB-X509.

CreatePlainCredential(string, string, SecureString)

Creates a PLAIN credential.

public static MongoCredential CreatePlainCredential(string databaseName, string username, SecureString password)

Parameters

databaseName string

Name of the database.

username string

The username.

password SecureString

The password.

Returns

MongoCredential

A credential for PLAIN.

CreatePlainCredential(string, string, string)

Creates a PLAIN credential.

public static MongoCredential CreatePlainCredential(string databaseName, string username, string password)

Parameters

databaseName string

Name of the database.

username string

The username.

password string

The password.

Returns

MongoCredential

A credential for PLAIN.

Equals(MongoCredential)

Compares this MongoCredential to another MongoCredential.

public bool Equals(MongoCredential rhs)

Parameters

rhs MongoCredential

The other credential.

Returns

bool

True if the two credentials are equal.

Equals(object)

Compares this MongoCredential to another MongoCredential.

public override bool Equals(object obj)

Parameters

obj object

The other credential.

Returns

bool

True if the two credentials are equal.

GetHashCode()

Gets the hashcode for the credential.

public override int GetHashCode()

Returns

int

The hashcode.

GetMechanismProperty<T>(string, T)

Gets the mechanism property.

public T GetMechanismProperty<T>(string key, T defaultValue)

Parameters

key string

The key.

defaultValue T

The default value.

Returns

T

The mechanism property if one was set; otherwise the default value.

Type Parameters

T

The type of the mechanism property.

ToString()

Returns a string representation of the credential.

public override string ToString()

Returns

string

A string representation of the credential.

WithMechanismProperty(string, object)

Creates a new MongoCredential with the specified mechanism property.

public MongoCredential WithMechanismProperty(string key, object value)

Parameters

key string

The key.

value object

The value.

Returns

MongoCredential

A new MongoCredential with the specified mechanism property.

Operators

operator ==(MongoCredential, MongoCredential)

Compares two MongoCredentials.

public static bool operator ==(MongoCredential lhs, MongoCredential rhs)

Parameters

lhs MongoCredential

The first MongoCredential.

rhs MongoCredential

The other MongoCredential.

Returns

bool

True if the two MongoCredentials are equal (or both null).

operator !=(MongoCredential, MongoCredential)

Compares two MongoCredentials.

public static bool operator !=(MongoCredential lhs, MongoCredential rhs)

Parameters

lhs MongoCredential

The first MongoCredential.

rhs MongoCredential

The other MongoCredential.

Returns

bool

True if the two MongoCredentials are not equal (or one is null and the other is not).