---
title: "How to enable and create roles in mvc?"  
description: "How to enable and create roles in mvc?"  
author: "Anonymous User"  
published: 2014-11-17  
updated: 2014-11-18  
canonical: https://www.mindstick.com/forum/12615/how-to-enable-and-create-roles-in-mvc  
category: "asp.net mvc"  
tags: ["c#", "mvc", "authentication", "authorization"]  
reading_time: 1 minute  

---

# How to enable and create roles in mvc?

How to enable [roles](https://www.mindstick.com/articles/1366/membership-roles-user-profile-in-asp-dot-net) in [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc)?? my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) is given below and I don't know how to create roles and I want to [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) it to the database..

```
[AttributeUsage(AttributeTargets.All)]    public class UserRightAttribute : ActionFilterAttribute    {        public override void OnActionExecuting(ActionExecutingContext filterContext)        {            //write your user right logic            //if user has right to do nothig otherwise redirect to error page.            string message = "It seems You  are not authorize to view this part of the web site!!!.";            RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();            redirectTargetDictionary.Add("area", "");            redirectTargetDictionary.Add("action", "SaveData");            redirectTargetDictionary.Add("controller", "Home");            redirectTargetDictionary.Add("customMessage", message);            filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);        }    }
```

## Replies

### Reply by Anonymous User

First web.config add following

<system.web>

<roleManager enabled="true" />

...

Adding roles is the same like ASP.NET for example

Roles.CreateRole("RoleName");

Roles.AddUserToRole("userName", "RoleName");


---

Original Source: https://www.mindstick.com/forum/12615/how-to-enable-and-create-roles-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
