1.8.5.Working with Roles

  • 1.Add RoleName.cs

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
    
      namespace Vidly2.Models
      {
          public class RoleName
          {
              public const string CanManageMovie = "CanManageMovie";
          }
      }
  • 2.Modify the movieController.cs

          public ActionResult Index()
          {
              if (User.IsInRole(RoleName.CanManageMovie))
              {
                  return View("List");
              }
              return View("ReadOnlyList");
          }
          [Authorize(Roles = RoleName.CanManageMovie)]
          public ActionResult New()
          {
              var genreTypes = _context.genreType.ToList();
              var viewModel = new MovieFormViewModel
              {
                  genreTypes = genreTypes
              };
              return View("MovieForm", viewModel);
          }
  • 3.Rename Movie/index.cshtml to list.cshtml

  • 4.Add a new file: ReadOnlyList.cshtml

Last updated