[MongoDB]: CSharp/.NET to test Auth

 

METHOD 1 :

using System;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

var connectionString = “mongodb://admin:pwd@dbversitydotcom/admin”;
MongoClient client = new MongoClient(connectionString);

MongoServer server = client.GetServer();
MongoDatabase db = server.GetDatabase(“admin”);
MongoCollection collection = db.GetCollection(“coll1”);
var res = collection.FindOneAs<BsonDocument>();
Console.WriteLine(res);

}
}
}

METHOD 2(USED THE SAME FOR SSL)

using System;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var credential = MongoCredential.CreateMongoCRCredential(“admin”, “admin”, “pwd”);

var sslSettings = new MongoClientSettings
{
Credentials = new[] { credential },
Server = new MongoServerAddress(“dbversitydotcom”, 27017)
};

MongoClient client = new MongoClient(sslSettings);
MongoServer server = client.GetServer();
MongoDatabase db = server.GetDatabase(“admin”);
MongoCollection collection = db.GetCollection(“coll1”);
var res = collection.FindOneAs<BsonDocument>();
Console.WriteLine(res);

}
}
}

  • Ask Question