[CSHARP-390] GetLastError doesn't work with authentication Created: 12/Feb/12  Updated: 02/Apr/15  Resolved: 12/Feb/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.3.1
Fix Version/s: 1.4

Type: Bug Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Note: this bug only applies when calling GetLastError explicitly. Using SafeMode.True works fine with authentication.

To reproduce:

var server = MongoServer.Create("mongodb://username:password@localhost");
var database = server["test"];
var collection = database["test"];
 
using (database.RequestStart())
{
    collection.Insert(new BsonDocument("x", 1));
    var result = server.GetLastError();
}

Produces the following stack trace:

System.InvalidOperationException: Connection requires credentials.
   at MongoDB.Driver.Internal.MongoConnection.CheckAuthentication(MongoDatabasedatabase) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal\MongoConnection.cs:line 243
   at MongoDB.Driver.MongoServer.AcquireConnection(MongoDatabase database, Boolean slaveOk) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoServer.cs:line 1126
   at MongoDB.Driver.MongoCursorEnumerator`1.AcquireConnection() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs:line 210
   at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs:line 223
   at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs:line 141
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   at MongoDB.Driver.MongoCollection.FindOneAs[TDocument](IMongoQuery query) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCollection.cs:line 513
   at MongoDB.Driver.MongoCollection`1.FindOne(IMongoQuery query) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCollection.cs:line 1660
   at MongoDB.Driver.MongoDatabase.RunCommandAs(Type commandResultType, IMongoCommand command) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoDatabase.cs:line 875
   at MongoDB.Driver.MongoDatabase.RunCommandAs[TCommandResult](IMongoCommand command) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoDatabase.cs:line 852
   at MongoDB.Driver.MongoServer.GetLastError() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoServer.cs:line 906
   at TestFree7051.Program.Main(String[] args) in c:\users\robert stam\documents\visual studio 2010\Projects\TestFree7051\Program.cs:line 23



 Comments   
Comment by Robert Stam [ 12/Feb/12 ]

The cause of the problem is that MongoServer.GetLastError sends the getLastError command to the admin database without using credentials. This works fine when authentication is off, but fails when it is on.

When using authentication, you must use the new MongoDatabase.GetLastError method instead of the MongoServer method, and it will use the credentials associated with your MongoDatabase instance.

So the failing code snippet above would be rewritten as:

using (database.RequestStart())
{
    collection.Insert(new BsonDocument("x", 1));
    var result = database.GetLastError(); // use database instead of server
}

Generated at Wed Feb 07 21:36:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.