title: Command find failed: not authorized on <dbname> to execute command { find:<collectionname>
body: <p>I am attempting to execute FindOne from my C# MVC ASP.net application. </p>
  
  <p>Unfortunately this fails with the following error: </p>
  
  <blockquote>
    <p>An unhandled exception of type 'MongoDB.Driver.MongoCommandException'
    occurred in MongoDB.Driver.Core.dll</p>
    
    <p>Additional information: Command find failed: not authorized on
    twitterstream to execute command { find: "tweets", filter: {}, limit:
    1, singleBatch: true }.</p>
  </blockquote>
  
  <p>If I start a MongoDB shell and run the following commands:</p>
  
  <pre><code>&gt; use twitterstream
  switched to db twitterstream
  &gt; db.auth("demouser", "abcd")
  1
  &gt; db.tweets.findOne()
  </code></pre>
  
  <p>The findOne() command executes and displays a record. </p>
  
  <p>What am I doing wrong or what step(s) have I missed within my C# application?</p>
  
  <p>To make the test case as small as possible I have reproduced this problem using a console app which is listed below:</p>
  
  <p>I am using MongoDB 3.4.6.</p>
  
  <p>The console app has dependencies on two NuGet packages; MongoDB.Driver (2.4.4) and mongocsharpdriver (2.4.4)</p>
  
  <p><strong>App.config</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
      &lt;startup&gt; 
          &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" /&gt;
      &lt;/startup&gt;
      &lt;appSettings&gt;    
        &lt;add key="MongoDatabaseName" value="twitterstream" /&gt;
        &lt;add key="MongoUsername" value="demouser" /&gt;
        &lt;add key="MongoPassword" value="abcd" /&gt;
        &lt;add key="MongoPort" value="27017" /&gt;
        &lt;add key="MongoHost" value="localhost" /&gt;
      &lt;/appSettings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p><strong>MongoContext.cs</strong></p>
  
  <pre><code>using System;
  using System.Configuration;
  using MongoDB.Driver;
  
  namespace DisplayingMongoConsoleApp
  {
      public class MongoContext
      {
          MongoClient _client;
          MongoServer _server;
  
          public MongoDatabase _database;
  
          public MongoContext()
          {
              // reading creditials from web.config file
              var MongoDatabaseName = ConfigurationManager.AppSettings["MongoDatabaseName"];
              var MongoUsername = ConfigurationManager.AppSettings["MongoUsername"];
              var MongoPassword = ConfigurationManager.AppSettings["MongoPassword"];
              var MongoPort = ConfigurationManager.AppSettings["MongoPort"];
              var MongoHost = ConfigurationManager.AppSettings["MongoHost"];
  
              // creating creditials
              var credential = MongoCredential.CreateMongoCRCredential(MongoDatabaseName, MongoUsername, MongoPassword);
  
              // creating MongoClientSettings
              var settings = new MongoClientSettings
              {
                  Credentials = new[] { credential },
                  Server = new MongoServerAddress(MongoHost, Convert.ToInt32(MongoPort))
              };
  
              _client = new MongoClient(settings);
              _server = _client.GetServer();
              _database = _server.GetDatabase(MongoDatabaseName);
  
          }
  
      }
  }
  </code></pre>
  
  <p><strong>TweetModel</strong></p>
  
  <pre><code>using MongoDB.Bson;
  using MongoDB.Bson.Serialization.Attributes;
  
  namespace DisplayingMongoConsoleApp
  {
      public class TweetModel
      {
          [BsonId]
          public ObjectId Id { get; set; }
  
          [BsonElement("thetweet")]
          public string Tweet { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>namespace DisplayingMongoConsoleApp
  {
      class Program
      {
          static void Main(string[] args)
          {
              MongoContext mongo = new MongoContext();
  
              var tweetDetails = mongo._database.GetCollection&lt;TweetModel&gt;("tweets").FindOne();
  
  
          }
      }
  }
  </code></pre>
  
  <p><strong>Update 1: Here are the roles demouser has:</strong></p>
  
  <pre><code>&gt; db.getUser("demouser")
  {
          "_id" : "twitterstream.demouser",
          "user" : "demouser",
          "db" : "twitterstream",
          "roles" : [
                  {
                          "role" : "readWrite",
                          "db" : "twitterstream"
                  },
                  {
                          "role" : "read",
                          "db" : "twitterstream"
                  }
          ]
  }
  </code></pre>