hyuindai_i30
hyuindai_i30

Reputation: 21

MongoDb.Driver. Authentication via SCRAM-SHA-1

i'm trying to establish a connection with Azure Cosmos DB (MongoDb). I use MongoDb.Driver v2.8.1. Could anyone help me ? Thanks a lot.

var configurationSection = _configuration.GetSection("MongoDb");

MongoUrl mongoUrl = new MongoUrl(configurationSection["connectionString"]);
var databaseName = configurationSection["databaseName"];
var userName = mongoUrl.Username;
var password = mongoUrl.Password;

MongoClientSettings settings = new MongoClientSettings();

settings.Server = mongoUrl.Server;
settings.UseSsl = true;
settings.SslSettings = new SslSettings();
settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12;
       
settings.ConnectTimeout = TimeSpan.FromMinutes(1);

MongoIdentity identity = new MongoInternalIdentity(databaseName, userName);
MongoIdentityEvidence evidence = new PasswordEvidence(password);

settings.Credential = new MongoCredential("SCRAM-SHA-1", identity, evidence);

var mongoClient = new MongoClient(settings);

The followong exception thrown when i trying to get data from a collection:

An unhandled exception occurred while processing the request.
MongoCommandException: Command saslContinue failed: SaslFailed. ConnectionId e6e7c4d7-4dcc-40c5-ad56-3307f3323a1a ActivityId: 9af84117-0000-0000-0000-000000000000.
    MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol<TCommandResult>.ProcessReply(ConnectionId connectionId, ReplyMessage<RawBsonDocument> reply)
    
MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.
        MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
        Stack Query Cookies Headers
        MongoCommandException: Command saslContinue failed: SaslFailed. ConnectionId e6e7c4d7-4dcc-40c5-ad56-3307f3323a1a ActivityId: 9af84117-0000-0000-0000-000000000000.
        MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol<TCommandResult>.ProcessReply(ConnectionId connectionId, ReplyMessage<RawBsonDocument> reply)
        MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol<TCommandResult>.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
        MongoDB.Driver.Core.Authentication.SaslAuthenticator.AuthenticateAsync(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)
        
        Show raw exception details
        MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.
        MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
        MongoDB.Driver.Core.Servers.Server.GetChannelAsync(CancellationToken cancellationToken)
        MongoDB.Driver.Core.Operations.FindOperation<TDocument>.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
        MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync<TResult>(IReadBinding binding, IReadOperation<TResult> operation, CancellationToken cancellationToken)
        MongoDB.Driver.MongoCollectionImpl<TDocument>.ExecuteReadOperationAsync<TResult>(IClientSessionHandle session, IReadOperation<TResult> operation, ReadPreference readPreference, CancellationToken cancellationToken)
        MongoDB.Driver.MongoCollectionImpl<TDocument>.UsingImplicitSessionAsync<TResult>(Func<IClientSessionHandle, Task<TResult>> funcAsync, CancellationToken cancellationToken)
        Babelfish.ME.MongoDb.Data.Services.AbstractDataService<TEntity>.FindAsync(BsonDocument filter, BsonDocument sort, int skip, int limit) in AbstractDataService.cs

Upvotes: 2

Views: 8523

Answers (3)

Nikita Thombre
Nikita Thombre

Reputation: 1

In my case, ImongoCollection was not getting the database name. So I added it manually from MongoClient and it worked for me.

Thanks

Upvotes: 0

hyuindai_i30
hyuindai_i30

Reputation: 21

Thank you very much. The problem was solved globally. Our company decided to use own MongoDb-Servers, not Azure Cosmos, because of costs. The following code works fine with own server. On the start i use SCRAM-SHA-1.

        var configurationSection = _configuration.GetSection("MongoDb");
        MongoUrl mongoUrl = new MongoUrl(configurationSection["connectionString"]);
        var connectionString = configurationSection["connectionString"];

        var mongoClient = new MongoClient(connectionString);

Upvotes: -1

cl0ud
cl0ud

Reputation: 1614

I connect to Azure CosmoDb Using MongoClientSettings.

   var mongoClientSettings = new MongoClientSettings()
        {
            Server = new MongoServerAddress(mongoConfig.Host, mongoConfig.Port),
            Credential = MongoCredential.CreateCredential(mongoConfig.Database, mongoConfig.Username, mongoConfig.Password),
            UseSsl = true,
            ReplicaSetName = "globaldb"
        };
   var client = new MongoClient(mongoClientSettings);

Obviously you would replace the config parts with your own values .

The only differences I can see is that you do not specify the ReplicaSetName and the fact that the authentication type that MongoCredential.CreateCredential returns is of type SCRAM-SHA-256 and yours is SCRAM-SHA-1

Upvotes: 0

Related Questions