Click here to Skip to main content
12,892,596 members (48,287 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 28 Jun 2014

MongoDB and .NET: Securing your MongoDB data with user authentication

, 28 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.

In this short article we will secure our MongoDB instance in our network with user authentication. This article is a continuation of a previous article that discussed implementing MongoDB as an alternative database engine for searching in our ASP.NET application. The article can be viewed here: However this article can be useful for anyone who has a MongoDB instance in their environment.

Note that my particular MongoDB instance is running on an Ubuntu Linux virtual machine but MongoDB runs on many different platforms including Windows Server. Also this article is a specific example of securing a MongoDB instance against an ASP.NET web application and chooses a the MongoDB CR Authentication over other authentication types because it is the only one supported by .NET applications. However you have other authentication options if you are not implemented .NET clients. For a more exhaustive list of options for securing your MongoDB instance visit the security documentation on the MonogDB web site:

1. Connect to the authentication database

You will want to create a user in MongoDB that maps to a real user or application. The user will be identified by a record in the user account store in MongoDB and authenticated to MongoDB with a password. To create a user in MongoDB that uses authentication, you must first create a user or system administrator account - that is an account that has permissions to create other user accounts and typically the first account that is created in your MongoDB instance for system administrators. First connect to the MongoDB instance using a mongo shell specifying the name of the authentication database that will hold the user accounts as "admin".

mongo --authenticationDatabase admin

2. Create a user with permissions to create other users

Then create a user account that will be used by system administrators for creating other user accounts or other administrative functions. For simplicity we will use a simple password but the actual password that you use should have a reasonable amount of lexicographical complexity.

use admin
   user: "siteUserAdmin",
   pwd: "site@dm1n",
   roles:[{role: "userAdminAnyDatabase",db: "admin"}]

The role userAdminAnyDatabase gives the account the ability to create user accounts in any database in the MongoDB instance. Now exit the mongo shell and reconnect using the system administrator account.

mongo -u siteUserAdmin -p password--authenticationDatabase admin

3. Create an account for each MongoDB application

Then we will create two user accounts for the footloosefs database (which is the database that will be used by my .NET applications). One account for the Footloose Financial Service web site will only have read access because that is all that it needs for searching MongoDB data and another account for the ETL program will need both read and write access.

use footloosefs
   user: "adminAppUser",
   pwd: "admin@dm1n",
   roles: [{ role: "read", db: "footloosefs" }]

use footloosefs
   user: "etlUser",
   pwd: "etl@dm1n",
   roles: [{ role: "readWrite", db: "footloosefs" }]

4. Restrict access to MongoDB to authorized users

Finally we need to configure our MongoDB instance to require authentication and effectively disable anonymous authentication. Be sure that you have created a superuser if necessary before you disable anonymous access. Open the file /etc/mongodb.conf using your favorite Linux editor and remove the "#" symbol from the following line and save the file:

auth = true

On Windows there will be a similar file called monogdb.cfg that will exist or you may have to create the file and add the "auth = true" line and specify the configuration file on the MongoDB Windows service commmand line. Then you will need to restart your MongoDB instance so that the changes take effect.

sudo service mongodb restart

5. Add MongoDB authentication to the ASP.NET web site and ETL program

Lastly lets look at how we will need to modify our .NET client applications now that our MongoDB instance requires user authentication.  

The code for my .NET applications are in a Visual Studio 2013 project at the following github address: I will be referring to code in the commit labeled "Modifications for MongoDB".

In the FootlooseFSDcoumentDBETL project open the App.config file and change the connection string to MongoDB so that it includes the username we setup for the ETL process, password, and database to which we are authenticating in the format below:

<add key="MongoDBConectionString" value="mongodb://etlUser:etl@dm1n@" />

Because we are storing a password in a configuration file, at this point we will want to encrypt the appSetting section of the configuration file. This article explains how this cane be done if you have never encrypted a .NET configuration file before:

In the FootlooseFS.Web.AdminUI project, we will want to do the same in the web.config file and encrypt the file.

This is the first step to secure our MongoDB environment and there are a few other actions that can be taken to make your MongoDB instance more secure including implementing SSL. All of these are documented in the Security section of the MongoDB online documentation. Thank you for reading.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Peter Carrasco
Team Leader
United States United States
I am a senior software engineer and technical lead for a mid-sized technology firm. I have an extensive background in designing, developing, and supporting ASP.NET web-based solutions. I am currently extending my skill-set to the area of application identity management and access control.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Lydia Gabriella16-Jul-14 10:51
memberLydia Gabriella16-Jul-14 10:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 28 Jun 2014
Article Copyright 2014 by Peter Carrasco
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid