2017-09-30 2 views
0

C#MVC ASP.netアプリケーションからFindOneを実行しようとしています。私はMongoDBのシェルを起動し、次のコマンドを実行した場合コマンドの検索に失敗しました:<dbname>でコマンドを実行できません{find:<コレクション名>

An unhandled exception of type 'MongoDB.Driver.MongoCommandException' occurred in MongoDB.Driver.Core.dll

Additional information: Command find failed: not authorized on twitterstream to execute command { find: "tweets", filter: {}, limit: 1, singleBatch: true }.

> use twitterstream 
switched to db twitterstream 
> db.auth("demouser", "abcd") 
1 
> db.tweets.findOne() 

findOne()コマンドが実行され、レコードを表示する

残念ながら、これは次のエラーで失敗します。

私は間違っていますか、私のC#アプリケーションの中で何が間違っていますか?

私は以下の通りであるコンソールアプリケーションを使用してこの問題を再現している可能な限りテストケースは限り小さくするには:

私はMongoDBの3.4.6を使用しています。

コンソールアプリケーションは、2つのNuGetパッケージに依存しています。 MongoDB.Driver(2.4.4)とmongocsharpdriver(2.4.4)

App.configファイル

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" /> 
    </startup> 
    <appSettings>  
     <add key="MongoDatabaseName" value="twitterstream" /> 
     <add key="MongoUsername" value="demouser" /> 
     <add key="MongoPassword" value="abcd" /> 
     <add key="MongoPort" value="27017" /> 
     <add key="MongoHost" value="localhost" /> 
    </appSettings> 
</configuration> 

MongoContext.cs

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); 

     } 

    } 
} 

TweetModel

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; } 
    } 
} 

Program.csの

namespace DisplayingMongoConsoleApp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      MongoContext mongo = new MongoContext(); 

      var tweetDetails = mongo._database.GetCollection<TweetModel>("tweets").FindOne(); 


     } 
    } 
} 

更新1:ここではありdemouserの役割は以下のとおりです。

> db.getUser("demouser") 
{ 
     "_id" : "twitterstream.demouser", 
     "user" : "demouser", 
     "db" : "twitterstream", 
     "roles" : [ 
       { 
         "role" : "readWrite", 
         "db" : "twitterstream" 
       }, 
       { 
         "role" : "read", 
         "db" : "twitterstream" 
       } 
     ] 
} 
+0

@Mate:更新中のdemouserにMongoの役割を追加しました –

+0

MongoDB.Driver(2.4.4) – Mate

答えて

1

最後に、このリンクのおかげで、この問題が解決しました。最も重要な段落だったこのページから

https://www.claudiokuenzler.com/blog/553/authentication-mongodb-3.x-failed-with-mechanism-mongodb-cr

enter image description here

一部をする:

MongoDB’s default authentication method is a challenge and response mechanism (SCRAM-SHA-1). Previously, MongoDB used MongoDB Challenge and Response (MONGODB-CR) as the default.

変更を加える前にdemouserを見直し、私は以下を参照してくださいだろう使用される資格情報はSCRAM-SHA-1です。

リンク、I無効認証の指示に従い、その後、モンゴで次のように使用して、私は認証の古い方法を使用するように私のMongoのインスタンスを変更シェル:、私は、認証を有効に

> use admin 
switched to db admin 
> var schema = db.system.version.findOne({"_id" : "authSchema"}) 
> schema.currentVersion = 3 
3 
> db.system.version.save(schema) 
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 

落とし、 demouserを再作成しました。資格情報にMONGODB-CRが使用されていることに注意してください。

enter image description here

私の質問に示すと予想通りfindOneコマンドが機能するようになりましたとしてコンソールアプリケーションの再放送。

1

モンゴドライバ:

<package id="MongoDB.Driver" version="2.4.4" targetFramework="net46" /> 

  var _conn = string.Format(@"mongodb://{0}:{1}@{2}:{3}/{4}" 
           , MongoUsername 
           , MongoPassword 
           , MongoHost 
           , MongoPort 
           , MongoDatabaseName); 

      var _client = new MongoClient(_conn); 
      var _database = _client.GetDatabase(MongoDatabaseName); 


      var twees = _database.GetCollection<Group>("tweets"); 
      var r = twees.AsQueryable().Select(x => x).ToList(); 

      Console.WriteLine(r.Count()); 
0123を試してみてください
+0

の回答を更新しましたが、このコードブロックは何をしていますか?別の方法を使用してデータベースに接続しようとしているのを見ることができます。この方法を使用する理由を説明すると役に立ちます。さらに、私の例に示すようにFindコマンドを使用していないのはなぜですか? –

関連する問題