How To Creating Default User Roles in ASP.NET MVC 5

5 (100%) 1 vote

BestASPNETHostingReview.com | Best and cheap ASP.NET MVC 5 hosting. ASP.NET MVC 5 is the latest update to Microsoft’s popular MVC (Model-View-Controller) technology – an established web application framework. MVC enables developers to build dynamic, data-driven web sites. MVC 5 adds sophisticated features like single page applications, mobile optimization, adaptive rendering, and more.

In this article, We’ll look into how to create default user roles in ASP.NET MVC 5. Let’s begin by establishing where the user role is assigned, and that is the registration stage. In the default template, you have the AccountController that contains a Register action. The default implementation looks like this:

What’s missing here is the role assignment, so let’s add that. Right after the CreateUserAndAccount call, we can check whether a specific role exists, and if it is – add the registered user to it. In case the role is new, create it.

Here I am working with a role called Standard, but obviously you can use another identifier for it. If you open the database that is carrying the app data, you will notice that there are two new tables introduced in the existing context – Roles and UsersInRoles.

As the data skeleton is established, you can now limit content access based on roles. In views, you could use the Authorize attribute:

That’s the tutorial How to create Default User Roles in ASP.NET MVC 5, for more information about ASP.NET MVC 5 Hosting please feel free to visit ASPHostPortal.com.