Home > DeveloperSection > Forums > How to enabling and creating roles in mvc`
jayprakash sharma
jayprakash sharma

Total Post:117

Points:821
Posted on    November-21-2014 11:18 PM

 ASP.NET MVC C#  Mvc4  Authentication  Authorization 
Ratings:


 1 Reply(s)
 1409  View(s)
Rate this:

How to enable roles in mvc?? my code is given below and i dont know how to create roles and i want to add it to the databse..

[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);

        }

    }



Pravesh Singh
Pravesh Singh

Total Post:101

Points:709
Posted on    November-22-2014 1:24 AM

Hi jay


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");


Modified On Apr-12-2016 12:17:51 AM

Don't want to miss updates? Please click the below button!

Follow MindStick