Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi All,

I am very new to developing an internet application using ASP.NET MVC4. I would like to use SimpleMembership for authentication and authorization in our application. Now I want to allow the Admin to define the roles from front end. and also assign those roles to the users while creating a user. How can we do that. in other words, How can we create User role and assign those roles to user using ASP.NET MVC4 Simple membership . Can any one help me out.

Thanks in advance.
Posted

To create a new role in ASP.NET MVC,maybe you can try this code :
@{
    ViewBag.Title = "RoleCreate";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
<div class="spacerBody">
    <p>&nbsp;</p>
    @Html.ActionLink("Roles", "RoleIndex") | @Html.ActionLink("Add Role to User", "RoleAddToUser")
<h2>Role Create</h2>

@using(Html.BeginForm()){
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <div>
    Role name</div>
    <p>
        @Html.TextBox("RoleName")
    </p>
    <input type="submit" value="Save" />
}
    </div>


After that,You can use this code to create a new Role into your database.
copy this code :
SQL
[Authorize(Roles = "Admin")]
        public ActionResult RoleCreate()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult RoleCreate(string RoleName)
        {

                Roles.CreateRole(Request.Form["RoleName"]);
                // ViewBag.ResultMessage = "Role created successfully !";

            return RedirectToAction("RoleIndex", "Account");
        }
 
Share this answer
 
Comments
srinathThanuku 7-Jan-14 8:56am    
Hi Jaden, Thanks for your answer. I am able to creating the roles and assign those to the users. But How can we differentiate the navigation based on those roles. Like I want Admin able to access all pages. And the non admin users will have access to some pages with deferent master page(Layout). How can we achieve this. It is really great to me, If you can send me some code snippet.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900