MongoDB 3.2 CSharp Driver 2.2 Testing with SSL & Kerberos Enabled

using System;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
protected static IMongoClient _client;
protected static IMongoDatabase _database;
public static void Main(string[] args)
{
var credential = MongoCredential.CreateGssapiCredential(“user@DBVERSITY.COM”);
var sslSettings = new MongoClientSettings
{
Credentials = new[] { credential },
UseSsl = true,
VerifySslCertificate = true,
Server = new MongoServerAddress(“hostname”, 32017)
};
try
{
_client = new MongoClient(sslSettings);
_database = _client.GetDatabase(“admin”);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.StackTrace);
}

PrintCollAsync().Wait(); /* Let’s wait for the PrintCollAsync execution */
Console.ReadLine(); /* And block the console */

}
private static async Task PrintCollAsync() /* This method should be static */
{
var _collection = _database.GetCollection<BsonDocument>(“collection1”);
var filter = new BsonDocument();
var count = 0;
using (var cursor = await _collection.FindAsync(filter))
{
while (await cursor.MoveNextAsync())
{
var batch = cursor.Current;
foreach (var document in batch)
{
Console.WriteLine(document.ToJson());
count++;
}
}
}
}
}
}

  • Ask Question