Hi,
Trying to run my application after adding role management code but i get this error.
cannot convert from 'Microsoft.AspNetCore.Identity.IdentityRole' to 'ExpenseTracker.Data.ApplicationUser'
public class UserRoleManagementController : Controller
{
private readonly UserManager<ApplicationUser> userManager;
private readonly RoleManager<ApplicationUser> roleManager;
public UserRoleManagementController(UserManager<ApplicationUser> userManager,
RoleManager<ApplicationUser> roleManager)
{
this.userManager = userManager;
this.roleManager = roleManager;
}
[HttpGet]
public IActionResult Index()
{
//get all users and send to view
var users = userManager.Users.ToList();
return View(users);
}
[HttpGet]
public async Task<IActionResult> Details(string userId)
{
//find user by userId
//Add UserName to ViewBag
//get userRole of users and send to view
var user = await userManager.FindByIdAsync(userId);
ViewBag.UserName = user.UserName;
var userRoles = await userManager.GetRolesAsync(user);
return View(userRoles);
}
[HttpGet]
public IActionResult AddRole()
{
return RedirectToAction(nameof(DisplayRoles));
}
[HttpPost]
public async Task<IActionResult> AddRole(string role)
{
//create new role using roleManager
//return to displayRoles
await roleManager.CreateAsync(new IdentityRole(role));
return RedirectToAction(nameof(DisplayRoles));
}
}
error on this line
await roleManager.CreateAsync(new IdentityRole(role));
in program.cs:
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
Thanks