mongodb认证后的远程连接

来源:互联网 发布:小学生的网络安全教育 编辑:程序博客网 时间:2024/05/18 19:19

创建认证模式:

mongod --auth -dbpath "D:\mongodb\data\db" -logpath "D:\mongodb\data\log"

添加用户:
db.addUser("admin", ",%F23_kj~00Opoo0+\/")
建立连接:

认证连接代码:

    //认证情况下的远程连接        public static void insert(TaskMode taskInfo)        {            var connectionString = "mongodb://192.168.0.1:27017";            var client = new MongoClient(connectionString);            var server = client.GetServer();            MongoCredentials credentials = new MongoCredentials("admin", "admin");            var database = server.GetDatabase("test", credentials);            var collection = database.GetCollection<Entity>("entities");            var entity = new Entity();            entity.Mode = taskInfo;            collection.Insert(entity);        }

        /// <summary>        /// 非认证情况下的本地连接        /// </summary>        /// <param name="taskInfo"></param>        public static void insert(TaskMode taskInfo)        {            var connectionString = "mongodb://localhost";            var client = new MongoClient(connectionString);            var server = client.GetServer();            var database = server.GetDatabase("test");            var collection = database.GetCollection<Entity>("entities");            var entity = new Entity();            entity.Mode = taskInfo;            collection.Insert(entity);        }


参考:http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial

Authentication

MongoDB supports a simple and straightforward authentication mechanism. You can read about it on thesecurity and authentication docs page.

The C# driver supports authentication in a couple of ways. As noted above in connection strings, you can specify default credentials on the connection string. The default credentials are always used as a fallback if no other credentials are supplied.

Supplying credentials can be done in two ways. First, they can be supplied to certain methods at runtime. These credentials will then be used to execute the desired functionality. The other, and more robust way, is to store credentials in a MongoCredentialsStore. MongoCredentials in the store are keyed by database, so if different databases require different users, then the credentials store is consulted first and, upon a miss, will fallback to the default credentials supplied on the connection string if they exist.

The example below uses the credential store to define admin credentials and credentials for the "foo" database. Access to databases other than "admin" or "foo" will use the connection string supplied default credentials "test".

var url = new MongoUrl("mongodb://test:user@localhost:27017");var settings = MongoClientSettings.FromUrl(url);var adminCredentials = new MongoCredentials("admin", "user", true);settings.CredentialsStore.Add("admin", adminCredentials);var fooCredentials = new MongoCredentials("foo", "user", false);settings.CredentialsStore.Add("foo", fooCredentials);var client = new MongoClient(settings);

GetServer method

You can navigate from an instance of a MongoClient to an instance of MongoServer by using the GetServer method.

MongoServer class

The MongoServer class is used to provide more control over the driver. It contains advanced ways of getting a database and pushing a sequence of operations through a single socket in order to guarantee consistency.

GetDatabase method

You can navigate from an instance of MongoServer to an instance of MongoDatabase (see next section) using one of the following GetDatabase methods or indexers:

  • MongoDatabase GetDatabase(MongoDatabaseSettings settings)
  • MongoDatabase GetDatabase(string databaseName)
  • MongoDatabase GetDatabase(string databaseName, MongoCredentials credentials)
  • MongoDatabase GetDatabase(string databaseName, MongoCredentials credentials, WriteConcern writeConcern)
  • MongoDatabase GetDatabase(string databaseName, WriteConcern writeConcern)

Sample code:

MongoClient client = new MongoClient(); // connect to localhostMongoServer server = client.GetServer();MongoDatabase test = server.GetDatabase("test");MongoCredentials credentials = new MongoCredentials("username", "password");MongoDatabase salaries = server.GetDatabase("salaries", credentials);

Most of the database settings are inherited from the server object, and the provided overloads of GetDatabase let you override a few of the most commonly used settings. To override other settings, call CreateDatabaseSettings and change any settings you want before calling GetDatabase, like this:

var databaseSettings = server.CreateDatabaseSettings("test");databaseSettings.SlaveOk = true;var database = server.GetDatabase(databaseSettings);

GetDatabase maintains a table of MongoDatabase instances it has returned before, so if you call GetDatabase again with the same parameters you get the same instance back again.


原创粉丝点击