在mongodb中为数据库添加新用户

时间:2012-04-17 10:47:50

标签: c# mongodb

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

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;

    }

当我使用现有数据库时,它会连接到该数据库,但是当我尝试添加新的数据库时,Add.UdUser会为此数据库提供错误的凭据' 请查看错误并回复

1 个答案:

答案 0 :(得分:1)

大多数人使用mongo shell来添加和删除用户,但是如果你真的想在C#中使用它,那么根据你想要做的事情,使用正确的凭据就是诀窍。假设您有以下两组凭据,一组用于管理数据库,另一组用于常规数据库:

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

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