Rameez Javed

Rameez Javed

  • NA
  • 395
  • 61.6k

Get ID of recently created User Role using Identity .net core 2.0

Sep 21 2020 11:57 PM
Hi, I have created new role using identity and want to get Id for this recently created Role on result.succeded.
I'm using the approach like below
  1. public async Task<IActionResult> CreateRole(CreateRoleViewModel model)  
  2. {  
  3. if (ModelState.IsValid)  
  4. {  
  5. IdentityRole identityRole = new IdentityRole  
  6. {  
  7. Name = model.RoleName  
  8. };  
  9.   
  10. IdentityResult result = await roleManager.CreateAsync(identityRole);  
  11.   
  12. if (result.Succeeded)  
  13. {  
  14.   // Here I want to get the RoleId created newly.
  15. return RedirectToAction("Index""Admin");  
  16. }  
  17. foreach(IdentityError error in result.Errors)  
  18. {  
  19. ModelState.AddModelError("", error.Description);  
  20. }  
  21. }  
  22.   
  23. return View();  
  24. }  
 Thanks in advance :)

Answers (1)