简体   繁体   中英

MongoDB.Driver.MongoAuthenticationException C# connection

I have this code based on a mongodb connection example, I am trying to insert an object. but getting and error on the insertone line

This is the error data: MongoDB.Driver.MongoAuthenticationException: 'Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.' Inner MongoCommandException: Command saslContinue failed: bad auth Authentication failed..

I am new using mongodb and got no idea of what is the error reason. Any help

static void Main(string[] args)
        {
            MongoClient dbClient = new MongoClient("mongodb+srv://dbUser:dbPass001@cluster0-pfry9.mongodb.net/test?retryWrites=true&w=majority");
            var database = dbClient.GetDatabase("test");

            var collection = database.GetCollection<anyClassTest>("entities");
            var r = new anyClassTest() { name="Original", edad = 36, id = 1};
            collection.InsertOne(r);
        }

        public class anyClassTest
        {
            public string name { get; set; }
            public int edad { get; set; }
            public int id { get; set; }
        }

For standalone, direct connections, create the client the following way,

    var servers = new List<MongoServerAddress>() { new MongoServerAddress("hostname", 27017) };
    var credential = MongoCredential.CreateCredential("admin", "user", "pass");
    var mongoClientSettings = new MongoClientSettings()
    {
        ConnectionMode = ConnectionMode.Direct,
        Credential = credential,
        Servers = servers.ToArray(), 
        ApplicationName = "NameOfApp",
    };

    MongoClient client = new MongoClient(mongoClientSettings);

Once you have the client (connected), then you can get the database / collections and do what you need.

var database = client.GetDatabase("dbname");
var collection = database.GetCollection<anyClassTest>("entities");
var r = new anyClassTest() { name="Original", edad = 36, id = 1};
collection.InsertOne(r);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM