1

我想向新创建的数据库添加一个新用户,如果该用户存在,那么我将连接到该数据库。我的代码是:

public CreateDatabaseOperationResult CreateDatabase(string databaseName,string username,string password, MongoServer server) {

        CreateDatabaseOperationResult createDatabaseOpResult = new CreateDatabaseOperationResult();
         string message = null;
         MongoCredentials credentials = new MongoCredentials(username, password);
         MongoUser user = new MongoUser(credentials, false);
         try
         {
             if (IsDatabaseNameValid(databaseName, out message))
             {
                 if (server.DatabaseExists(databaseName, admincredentials) == true)
                 {
                     createDatabaseOpResult.Database = server.GetDatabase(databaseName, credentials);
                     MongoUser tempuser = createDatabaseOpResult.Database.FindUser(username);
                     if (tempuser.Equals(user))
                     {

                         //createDatabaseOpResult.DatabaseExists = true;
                         createDatabaseOpResult.IsOperationSuccessfull = false;
                         throw new ArgumentException("Database Already exist with different set of credentials ");
                     }
                 }
                 else
                 {
                     createDatabaseOpResult.Database = server.GetDatabase(databaseName, credentials);
                     createDatabaseOpResult.Database.AddUser(user);
                     //createDatabaseOpResult.DatabaseExists = false;

                 }

                 createDatabaseOpResult.IsOperationSuccessfull = true;
             }
         }
         catch (MongoQueryException ex)
         {
             createDatabaseOpResult.Error = ex;
         }
         //catch (MongoAuthenticationException ex)
         //{
         //    createDatabaseOpResult.Error = ex;
         //}
         catch (MongoException ex)
         {
             createDatabaseOpResult.Error = ex;
         }
         catch (ArgumentException ex)
         {
             createDatabaseOpResult.Error = ex;
         }


        return createDatabaseOpResult;

    }

当我使用现有数据库时,它连接到该数据库,但是当我尝试添加新的使用 Database.AddUser 时给出错误“此数据库的凭据无效”请查看错误并回复

4

1 回答 1

2

大多数人使用 mongo shell 来添加和删除用户,但如果您真的想在 C# 中执行此操作,诀窍是根据您要执行的操作使用正确的凭据。假设您有以下两组凭据,一组用于 admin 数据库,一组用于常规数据库:

var adminCredentials = new MongoCredentials("myadminusername", "myadminpassword", true);
var userCredentials = new MongoCredentials("myusername", "myuserpassword");

请注意,在创建 adminCredentials 时,您必须将 true 传递给 admin 参数。

要测试数据库是否存在需要管理员凭据:

if (server.DatabaseExists("mydatabase", adminCredentials))
{
    // database exists
}

添加用户需要管理员凭据:

var myDatabaseWithAdminCredentials = server.GetDatabase("mydatabase", adminCredentials);
if (myDatabaseWithAdminCredentials.FindUser("myusername") == null)
{
    myDatabaseWithAdminCredentials.AddUser(userCredentials);
}

通常,您使用常规用户凭据来处理数据库:

var myDatabaseWithUserCredentials = server.GetDatabase("mydatabase", userCredentials);
var count = myDatabaseWithUserCredentials.GetCollection("mycollection").Count();

另外,请记住,每个数据库都可以有任意数量的用户,因此您实际上不需要检查数据库是否已经存在使用不同的凭据集。

于 2012-04-17T14:16:55.380 回答