ASP.NET Core Identity with Cosmos DB (MongoDB) Part1: Database Configuration
Hello folks this time I bring to you a very simple an useful tutorial to show you how fast you can start creating awesome services and apps using the .NET framework and in this case a NoSQL database.
This will be the first of 3 post series about how to implement ASP.NET Core Identity with Cosmos DB (MongoDB) and expose it over a Web API, in this first blog we will see the configuration of the database to use both a local instance and Cosmos DB of MongoDB for live version.
Let's start first revising what is? and why? MongoDB
MongoDB is free and open source NoSQL database and is very popular in a lot of companies because his paradigm allow to quickly scale large and modern apps, let's analyze some points from MongoDB webpage:
What is NoSQL?
NoSQL encompasses a wide variety of different database technologies that were developed in response to the demands presented in building modern applications:
- Developers are working with applications that create massive volumes of new, rapidly changing data types — structured, semi-structured, unstructured and polymorphic data.
- Long gone is the twelve-to-eighteen month waterfall development cycle. Now small teams work in agile sprints, iterating quickly and pushing code every week or two, some even multiple times every day.
- Applications that once served a finite audience are now delivered as services that must be always-on, accessible from many different devices and scaled globally to millions of users.
- Organizations are now turning to scale-out architectures using open source software, commodity servers and cloud computing instead of large monolithic servers and storage infrastructure.
As you noticed we have a lot of benefits using a NoSQL database achieving the demands and scalability of modern applications.
MongoDB Local Configuration
After reviewing the benefits of NoSQL database let start creating our local configuration.
We will download and installing the MongoDB in out machines in this case I'll use a MacOSX machine if you have linux or Windows you can follow the installation steps here.
We will use Homebrew for a quick and fast installation if you didn't have it installed you can get it here.
You can see if you have it installed by typing "brew --version" in your terminal if everything is ok you'll see the next output:
After that we will be good to start installing MongoDB in our machines with the next commands
First lets update brew references using next command "brew update" this will take a while since it will download all packages references data.
After complete that command we will be able to run the "brew install mongodb" in order to install MongoDB binaries this will take also a moment if in any case you get any permission error you can try using "sudo" prefix on the command as follow "sudo brew install mongodb"
after the install command get completed now we will be able to run next command "mongod" to confirm that MogoDB is just installed if everything goes well you'll see the next message
"[initandlisten] waiting for connections on port 27017"
starting here we will use RoboT3 that is a lighweight UI tool for MongoDB managment you can get it here.
Now let's configure RoboT3 to se our local database once you open the tool you'll see a screen like this:
lets configure our local connection just to test that everything is ready to go on our local machine you notice that by default is using the localhost values since we didn't configure anything related to security if we hit the test button we will see a confirmation message that everything is ready to go:
If you can see that confirmation dialog now we're able to Save the configuration and get into the next screen lets click on Connect button:
now you will be able to see all default databases into our machine:
We're almost done on our local configuration lets create a database with a right click on our instance connection in this case "Local Connection" and clicking on Create Database, to use in our example app we will use the name "awesomedatabase"
Now you're able to see our new database details
Awesome at this point we completed the basic config on out local machine lets continue creating a Cosmos DB configuration on our Azure portal.
Cosmos DB Azure Configuration
Cosmos DB is the Azure service that allow you quickly configure a distributed databases using common databases engines like MSSQL & MongoDB.
From Cosmos DB docs webpage we can see next definition:
Azure Cosmos DB is a globally distributed, multi-model database service that supports document, key-value, wide-column, and graph databases. Learn how to use Azure Cosmos DB with our quickstarts, tutorials, and samples.
Now lets move to our azure portal if you don't have an account you can create a FREE account here https://azure.microsoft.com/en-us/free/
as you probably seen CosmosDB is free with a limited quota first 12 months of you create your subscription this will be enough to play with it
Is time to open our azure portal here https://portal.azure.com
And in the left side of the dashboard we will expand it and click on create new resource button:
and let's search or select "CosmosDB"
after that we will see a form with the details to create our new Cosmos DB service:
Fill the form using the mandatory data
ID: unique name used to reference you cosmos db service.
API: Database engine for out demo purposes we will select MongoDB.
Subscription: Your target azure subscription.
Resource Group: Main resource group the service will be tied you can select a created one or create a new one.
Location: Where your database service will be located.
Also you are able to configure geo-redudandy, multi-region and virtual network configuration if is needed.
after you select your config you can click on Create button, the deployment task will be perform by azure dashboard and after a couple minutes you'll get the notification about a completed task.
Now you can pin the item to your dashboard if you want of you go to the resource config, and you will see your resource config as follow.
That's all now we have the basic config, we can just go to the Quick Start menu option to see the connection string that we will need later in the second blog part:
We're done with our basic databases configuration part at this moment we can recap our journey.
- Install MongoDB in our machine
- Install RoboT3 to manage our database.
- Create a local connection in our RoboT3 software.
- Test Connection and Create a new database.
- Create an Azure free account.
- Search and create a new Cosmos DB Instance.
- Open the quick start section on azure cosmos db dashboard to see our connection string.
In the next part we can start creating an ASP.NET Core Web Api to connect our Identity Service with the databases that we just created.
See you soon in the next part.