unable to authenticate with mongodb golang driver - TagMerge
5unable to authenticate with mongodb golang driverunable to authenticate with mongodb golang driver

unable to authenticate with mongodb golang driver

Asked 5 months ago
1
5 answers

Add ssl=false to your uri. Worked for me

Source: link

0

You could try using 'options.Credential' to pass the authentication settings.

Seems like a cleaner way than formatting an URI that needs to be parsed later on.

https://docs.mongodb.com/drivers/go/current/fundamentals/auth/

clientOpts := options.Client().SetHosts(
    []string{"localhost:27017"},
).SetAuth(
    options.Credential{
        AuthSource: "<authenticationDb>",
        AuthMechanism: "SCRAM-SHA-256",
        Username: "<username>",
        Password: "<password>",
    }
)
client, err := mongo.Connect(context.TODO(), clientOpts)

Source: link

0

Based on MongoDB documentation for the authentication process, there is a parameter to identify which database is used for authentication besides the target database on the URI.

While in mongoshell you can use this line

mongo "mongodb://Admin:${DBPASSWORD}@<host>:<port>/admin?authSource=admin"

I used that information to add ?authSource=admin to my CONNECTION_URL

CONNECTION_URL=mongodb://root:example@mongo:27017/my_database?retryWrites=true&w=majority&authSource=admin

That worked for me. Hope it does for you too.

For detailed information please review https://www.mongodb.com/features/mongodb-authentication

Source: link

0

Once I added the next steps
public class MongoDbSettings
{
        public string Host { get; init; }
        public int Port { get; init; }
        public string User { get; set; }
        public string Password { get; set; }

        public string ConnectionString 
        { 
            get
            {
                return $"mongodb://{User}:{Password}@{Host}:{Port}";
            } 
        }
}
public class MongoDbSettings { public string Host { get; init; } public int Port { get; init; } public string User { get; set; } public string Password { get; set; } public string ConnectionString { get { return $"mongodb://{User}:{Password}@{Host}:{Port}"; } } }
"MongoDbSettings" : 
  {
    "Host" : "localhost",
    "Port" : "27017",
    "User" : "mongoadmin"
  }
and added this <UserSecretsId>d2be828b-2f4d-4008-bb0b-75ab11b1f2ed</UserSecretsId> using this
dotnet user-secrets init
dotnet user-secrets set MongoDbSettings:Password password

Source: link

0

The issue is you are authenticating the mongoadmin user for clientdb. But mongoadmin user is authenticated to admin db. Get the mongoadmin user authenticated and then you can access clientdb.
IMongoClient _client;
        public readonly IMongoDatabase _database;

        public MongoContext_URIBased()
        {
            var mongoUrl = new MongoUrl("mongodb://mongoadmin:secret@172.17.103.158:2020/admin");
            _client = new MongoClient(mongoUrl);
            _database = _client.GetDatabase("clientdb");
        }

Source: link

Recent Questions on mongodb

    Programming Languages