How to create Default User Roles in ASP.NET MVC 5

How to create Default User Roles in ASP.NET MVC 5

CheapASPNETHostingREview.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.

mvc

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:

Or you could check for the role directly:

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.

ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, .NET 4.5.1/ASP.NET 4.5, ASP.NET MVC 5.0/4.0, Silverlight 5 and Visual Studio Lightswitch. Click here for more information.