1

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.

AbdulRahman Ansari
  • 3,007
  • 1
  • 21
  • 29
Stefan
  • 33
  • 8

1 Answers1

0

You can use IdentityRole class 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 = rolemanager

To create new roles, use IdentityRole in your view and in your controller

public IActionResult AddRole()
{
    var role = new IdentityRole
    {
        Name = "Customer",  //Name will be sufficient
    };
    _roleManager.CreateAsync(role); //role actually comes as parameter from view
    return View();
}

On 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).

public IList<IdentityRole> GetAllRoles()
{
    return _roleManager.Roles.ToList();
}

Finally, while adding/updating user you should now assign them to the roles.

//single
_userManager.AddToRoleAsync(user, role);  //role should be role name

//multiple
_userManager.AddToRolesAsync(user, roles); // roles should be list of role names
rabink
  • 679
  • 8
  • 13