HOW TO CREATE ROLE AND ENABLE ROLE IN MVC?

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
 1102  View(s)
Ratings:
Rate this:

How to enable roles in mvc?? my code is given below and i don't know how to create roles and i want to add 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);
            }
        }

  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: How to create role and enable role in mvc?

    First web.config add following.

    <system.web>
        <roleManager enabled="true">
          ....
        </roleManager>
      </system.web>

    Adding roles is the same like ASP.NET for example

    Roles.CreateRole("RoleName");
    Roles.AddUserToRole("userName", "RoleName");

      Modified On Apr-05-2018 06:52:13 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!