Maybe I missed something, but I read a bunch of documentation and articles about authentication and authorization using .NET Core 2.0, but I did not find anything regarding user management.
What I'm trying to achieve is to have an admin user interface that can create Users, list all existing users and assign them to the predefined role (s) and / or predefined policies.
I tried unsuccessfully to do this without any luck (I am having problems trying to use the model, for example IEnumerable<IdentityUser>
for an invalid constructor:
InvalidOperationException: A suitable constructor for type 'System.Collections.Generic.IEnumerable`1 [Microsoft.AspNetCore.Identity.IdentityUser]' could not be found. Make sure that the type is specific and that the services are registered for all parameters of the public constructor.
and i cant get RoleManager
in any controller. It works great with UserManager, but never with RoleManager. I added
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
to run, so I taught that it will be automatically entered DI ...
ApplicationUser
defined as follows:
namespace KaraokeServices.Data
{
public class ApplicationUser : IdentityUser
{
}
}
I defined a UserController, for example:
namespace KaraokeServices.Controllers
{
[Route("[controller]/[action]")]
public class UserController : Controller
{
private readonly UserManager<ApplicationUser> userManager;
public UserController(ApplicationDbContext pContext, SignInManager<ApplicationUser> pSignInManager, ILogger<AccountController> logger)
{
userManager = pSignInManager.UserManager;
}
[HttpGet]
public IActionResult Index()
{
List<ApplicationUser> users = new List<ApplicationUser>();
users = userManager.Users.ToList();
return View(users);
}
}
}
And here is the user /Index.cshtml
@page
@model IEnumerable<ApplicationUser>
@{
ViewData["Title"] = "Gestion des utilisateurs";
}
<h2>@ViewData["Title"]</h2>
<form method="post">
<table class="table">
<thead>
<tr>
<th>Courriel</th>
<th>Roles</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var user in Model)
{
<tr>
<td>@user.Email</td>
<td></td>
<td>
<a asp-page="./Edit" asp-route-id="@user.Id">Éditer</a>
<button type="submit" asp-page-handler="delete" asp-route-id="@user.Id">
Effacer
</button>
</td>
</tr>
}
</tbody>
</table>
<a asp-page="./Create">Créer</a>
</form>
but I always make a mistake ...
How am I wrong?