In MVC 5 applications you may want to seed the database with initial users for various reasons. You may want default users and roles added as part of the application. In this post, we will take a look at how to seed the database with default users.
The MVC 5 application uses ASP.Net Identity 2.0 to manage users, roles and identities. ASP.Net Identity 2.0 uses the Entity framework code first approach to create a database. To seed the database, first you need to enable migration on the database. To enable migration, in the package manager console run the command enable-migrations as shown below:
After successfully enabling migration you should find a Migrations folder in the solution explorer.
ASP.NET MVC 5 provides five different kinds of Filters. They are as follows:
- Authentication [Introduced in MVC5]
Filters are used to inject logic at the different levels of request processing. Let us understand where at the various stages of request processing different filters get applied.
- Authentication filter runs before any other filter or action method
- Authorization filter runs after Authentication filter and before any other filter or action method
- Action filter runs before and after any action method
- Result filter runs before and after execution of any action result
- Exception filter runs only if action methods, filters or action results throw an exception
Recently I attended Cloud Camp by Microsoft India in Delhi. I had good time learning at the event. Besides other Microsoft Azure services like Azure Websites, Azure Mobile Services and Azure DocumentDB impressed me lot. In this post, I am sharing first level learning on Azure DocumentDB.
Some of the important points about the DocumentDB is as follows:
- It is a schema free database
- It has support of rich query and transaction processing
- It works with JSON based data model
- It works on HTTP protocol
In the last post I talked about How to get current user in ASP.Net MVC 5 . Type of the current user is IPrincipal which is shown as follows:
You may come across requirement to work with ApplicationUser which is created as follows:
You can covert returned User to ApplicationUser as follows:
ApplicationDbContext db = new ApplicationDbContext(); ApplicationUser appUser = db.Users.FirstOrDefault(x => x.Id == userId); return appUser ;
To convert create instance of the ApplicationDBContext and then fetch user using LINQ to Entity. Returned user from context is of type ApplicationUser.
Hope it helps. Happy coding.