ASP.NET Core 2.0中的用户管理

5
也许我漏掉了什么,但是我阅读了大量关于使用.NET Core 2.0进行身份验证和授权的文档和文章,但是没有找到任何关于用户管理的内容。
我的目标是拥有一个管理员用户界面,可以创建用户、列出所有现有用户并将它们分配给预定义的角色和/或预定义的策略。
我尝试过在不运用IEnumerable<IdentityUser>模型的情况下实现此目标,但没有成功,因为出现了无效构造函数的问题:

InvalidOperationException: A suitable constructor for type 'System.Collections.Generic.IEnumerable`1[Microsoft.AspNetCore.Identity.IdentityUser]' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

而且我无法在任何控制器中获取RoleManager。虽然UserManager可以正常工作,但RoleManager从未成功添加。
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

针对这个新创企业,我认为依赖注入会自动完成,所以我进行了教学...

ApplicationUser的定义如下:

namespace KaraokeServices.Data
{
    public class ApplicationUser : IdentityUser
    {
    }
}

我定义了一个名为UserController的控制器,如下所示:
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);
        }
    }
}

以下是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>

但我总是遇到错误...

我做错了什么?


请添加“ApplicationUser”的定义。 - SpruceMoose
我刚刚编辑了帖子以添加特定信息。ApplicationUser以及UserController。 - Redge
1
你需要添加包含 AddUserStore()AddRoleStore() 的代码,参见 Issues with injecting RoleManager - SpruceMoose
1个回答

1
代替 <\p>
services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

添加经理们。
services.AddIdentity<User, Role>()
                .AddEntityFrameworkStores<AuthContext>()
                .AddUserManager<UserManager>()
                .AddSignInManager<SignInManager>()
                .AddDefaultTokenProviders();

你可以尝试使用 @model ICollection<ApplicationUser> 代替吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接