What is Role Based Authentication In ASP.NET MVC?

Role Based Authentication is Membership and Role providers. These providers allows us to define Roles, Users and assign roles to users which helps us to manage Authorization. But with an increase in social networking and global authentication providers, we needed an upgraded membership system. ASP.NET Identity is the new membership system for building ASP.NET web applications, phone, store, or hybrid applications using social identities for authentication and authorization. So we can now use Windows Live (e.g. Hotmail), Gmail, Facebook and Twitter for authentication before the user starts using our web application.

Adding role checks

Role-based authorization checks are declarative—the developer embeds them within their code, against a controller or an action within a controller, specifying roles which the current user must be a member of to access the requested resource.

Role Based Authentication In ASP.NET MVC
For example, the following code limits access to any actions on the AdministrationController to
users who are a member of the Administrator role:
  1. [Authorize(Roles = “Administrator”)]
  2. public class AdministrationController : Controller
  3. {
  4. }

For multiple roles as a comma separated list

  1. [Authorize(Roles =”Admin,Employee”)]
  2. public ActionResult Index()
  3. {
  4. var employees = db.Employees.Include(e => e.Department);
  5. return View(employees.ToList());
  6. }

If you apply multiple attributes then an accessing user must be a member of all the roles
specified; the following sample requires that a user must be a member of both the PowerUser
and ControlPanelUser role.

  1. [Authorize(Roles = “PowerUser”)]
  2. [Authorize(Roles = “ControlPanelUser”)]
  3. public class ControlPanelController : Controller
  4. {
  5. }
In the project add a new folder called CustomFilters and add the class file with following login logic
in it:
  1. using System.Web.Mvc;
  2. namespace A11_RBS.CustomFilters
  3. {
  4. public class AuthLogAttribute : AuthorizeAttribute
  5. {
  6. public AuthLogAttribute()
  7. {
  8. View = “AuthorizeFailed”;
  9. }
  10. public string View { get; set; }
  11. /// <summary>
  12. /// Check for Authorization
  13. /// </summary>
  14. /// <param name=”filterContext”></param>
  15. public override void OnAuthorization(AuthorizationContext filterContext)
  16. {
  17. base.OnAuthorization(filterContext);
  18. IsUserAuthorized(filterContext);
  19. }
  20. /// <summary>
  21. /// Method to check if the user is Authorized or not
  22. /// if yes continue to perform the action else redirect to error page
  23. /// </summary>
  24. /// <param name=”filterContext”></param>
  25. private void IsUserAuthorized(AuthorizationContext filterContext)
  26. {
  27. // If the Result returns null then the user is Authorized
  28. if (filterContext.Result == null)
  29. return;
  30. //If the user is Un-Authorized then Navigate to Auth Failed View
  31. if (filterContext.HttpContext.User.Identity.IsAuthenticated)
  32. {
  33. // var result = new ViewResult { ViewName = View };
  34. var vr = new ViewResult();
  35. vr.ViewName = View;
  36. ViewDataDictionary dict = new ViewDataDictionary();
  37. dict.Add(“Message”, “Sorry you are not Authorized to Perform this Action”);
  38. vr.ViewData = dict;
  39. var result = vr;
  40. filterContext.Result = result;
  41. }
  42. }
  43. }
  44. }

custom filter is derived from AuthorizeAttribute class and overrides the OnAuthorization() method. The IsUserAuthorized() helper method checks the user authentication with AuthorizationContext class. If the Result property returns null, then the
Authorization is successful and user can continue the operation; else if the user is authenticated but not authorized, then an Error Page will be returned.

If you’re using MVC 5 you have to enable lazy loading in your DbContext by putting the
following line in your DbContext initialisation.

this.Configuration.LazyLoadingEnabled = true;

Authentification with Active Directory (AD) with Asp.Net

Author – Disha Raval

Get a Quote