Community Post

Spring MVC – Component-Scan, Annotation-Config, Annotation-Driven

Ethan Millar

Experts of asp.net MVC Development Company will explain how to retrieve the Navigation links from the SQL database tables Entity framework. They have shared all basic info about the dynamic Role in this post.

This Article will explain the Dynamic role based navigation based on the user login. Most of MVC breadcrumbs uses Sitemap Provider with static Sitemap Nodes. Here I am going to explain the how we can retrieve the Navigation links from the SQL database tables through Entity framework.

Roles will be stored in the separate table along with userID.And Navigation Urls will be stored in the separate table with roles. So whenever user logged in, the system will read the roles for the particular user, and based on the role, the navigation link will display.

And also Roles have been set on the particular controller as attribute,this will avoid the user to redirect to the restricted link. So if the user try to navigate to the particular page not even available on the Breadcrumb, it will automatically redirect to the login page again.

Create the Roles Entity. First need to crate the Table for storing the role value in SQL server.

After creating the Table , create the ADO.Net Entity modle for the particular table. Select new item as in the below screen.

Choose the table you want to create the Entity model.

In this model, user ID is added for relating the roles for the particular login user.

Kindly do the above step for create the Menu Model, to store the Navigation links. In thisModelRoleID will be captured to get the link for the particular role, so based on roles available for the logged in user the navigation link will be rendered on the page.

For assigning the roles to the login users, new custom class has been created (AssignRoles) This class has been inherited from RoleProvider base class.

public class AssignRoles: RoleProvider
    {
public override string[] GetRolesForUser(string username)
        {
string[] retVal = new string [1];
using (Entities2 RolesContext = new Entities2())
            {
var Roles = RolesContext.Roles.FirstOrDefault(x =>x.UserID == username);
if (Roles != null)
                {
retVal[0] = Roles.RoleCode;
                 }               

            }
returnretVal;
        }

Add the following code in Web Config to register the role manager.

<roleManagerdefaultProvider="CustomRoleProvider" enabled="true" cacheRolesInCookie="false">
<providers>
<clear />
<add name="CustomRoleProvider" type="DynamicBreadCrumbs.Models.AssignRoles,DynamicBreadCrumbs" />
</providers>
</roleManager>

For rendering the dynamic Navigation from Entity model, place the below code in “_Layout.cshtml” file.

<ul id="menu">
@if (HttpContext.Current.User.Identity.IsAuthenticated)
{
String[] roles = Roles.GetRolesForUser();
stringroleid = roles[0];

using (DynamicBreadCrumbs.MenuDBContextMenusContext = new DynamicBreadCrumbs.MenuDBContext())
            {
                  /* varNavUrls = from item in MenusContext.Menus
whereitem.Role.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries) 
                    .Any(x =>roles.Contains(x) || x == "All")
select item;*/
foreach (varNavUrl in MenusContext.Menus.Where(x=>x.Role == roleid || x.Role == "All") )
                {
                    @: <li>@Html.ActionLink(NavUrl.Text, NavUrl.Action,NavUrl.Controller)</li>
                }
}

}
</ul>

Role mapping with Users Table Data.

Menu Link Data

Add the below attribute in the controller for Role based access. Here I am adding supplier Admin role as an attribute to the Supplier controller. He can add, edit and delete the supplier records. Roles All is used for display the anonyms access like Index, about and contact pages will be displayed to the all the users, even if they have low privilege.

[Authorize(Roles = "Supplier")]
publicActionResult Index()
        {
return View(db.SupplierDbContext.ToList());
        }

When login with user ID RAM, Supplier page navigation will display along with anonyms pages, but customer link will not show, since it is not having access to the role.

When user “Raju” logged in Supplier menu will not be available, but customer link will be available.

So, the experts of asp.net mvc development company have just explained how to create the Menu navigation link dynamically that is based on Roles assigned to the user login. If you want to ask anything related to the subject, make comments below

Conclusion

This article have explained how we can create the Menu navigation link dynamically based on the Roles assigned to the login user.

Normally Static menu links have been defined in the _Layout page. Adding and updating the menu navigation is very difficult, which means every time we need to touch the coding part. And also displaying the menus, based on the user permissions that can be only be achieved through Integration of Role manger and Dynamic Menu rendering. This has been explained in easy steps and code sample. Hope you enjoy by reading this article. Thanks for reading this article.

Ethan Millar

I am a technical writer for the last nine years at Aegis Softtech. I especially write articles related to Java, support, services and latest updates .