Creating Roles in Asp.net Identity MVC 5

前端 未结 11 1416
情深已故
情深已故 2020-12-02 05:09

There is very little documentation about using the new Asp.net Identity Security Framework.

I have pieced together what I could to try and create a new Role and add

11条回答
  •  孤街浪徒
    2020-12-02 05:43

    Roles View Model

    public class RoleViewModel
    {
        public string Id { get; set; }
        [Required(AllowEmptyStrings = false)]
        [Display(Name = "RoleName")]
        public string Name { get; set; }
    }
    

    Controller method

        [HttpPost]
        public async Task Create(RoleViewModel roleViewModel)
        {
           if (ModelState.IsValid)
           {
               var role = new IdentityRole(roleViewModel.Name);
               var roleresult = await RoleManager.CreateAsync(role);
               if (!roleresult.Succeeded)
               {
                   ModelState.AddModelError("", roleresult.Errors.First());
                   return View();
               }
               return RedirectToAction("some_action");
           }
           return View();
        }
    

提交回复
热议问题