I've created an small fleet management app and with the help of tutorials and materials I've found on the Microsoft website and other.
I managed to implement the User Management services (did not create the project with Authentication from the start) and can now create new Users and manage them from the interface, although I created an "Admin" Role and assigned it to admin account (through Startup.cs) , I'm having a hard time creating an UI to manage the Roles (create, modify, delete, assign roles to users). I've searched for 3 days already and cannot find a proper tutorial on how to use the RoleManager to create a controller and views for it.
Can anyone point me in the right direction?
I'm using ASP.NECore.All 2.0.6 & EF Core 2.1.1
The AccountController:
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ILogger _logger;
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ILogger<AccountController> logger)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_logger = logger;
}
The ManageController:
public class ManageController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ILogger _logger;
private readonly UrlEncoder _urlEncoder;
private const string AuthenticatorUriFormat = "otpauth://totp/{0}:{1}?secret={2}&issuer={0}&digits=6";
private const string RecoveryCodesKey = nameof(RecoveryCodesKey);
public ManageController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ILogger<ManageController> logger,
UrlEncoder urlEncoder)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_logger = logger;
_urlEncoder = urlEncoder;
}
The rest of the actions are the default ones, that you get when creating a new project with Individual Accounts Authentication.
You can use
IdentityRoleclass which is provided by the asp.net core identity implementation. First, add the role manager to your class and inject it.private readonly RoleManager<IdentityRole> _roleManager;Pass
RoleManager<IdentityRole>in your constructor and add_rolemanager = rolemanagerTo create new roles, use
IdentityRolein your view and in your controllerOn your user creation and update views you can read all the roles and create a dropdown list (use multiselect to assign more than one roles).
Finally, while adding/updating user you should now assign them to the roles.