也许我想念一些东西但是,我阅读了大量关于使用.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.
而且我无法在任何Controller中获得RoleManager.它适用于UserManager,但从不使用RoleManager.我补充道
services.AddIdentity<ApplicationUser,IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders();
到初创公司,所以我教它将自动注入DI …
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>
但我总是被这个错误所困扰……
我做错了什么?
解决方法
代替
services.AddIdentity<ApplicationUser,IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders();
添加经理
services.AddIdentity<User,Role>() .AddEntityFrameworkStores<AuthContext>() .AddUserManager<UserManager>() .AddSignInManager<SignInManager>() .AddDefaultTokenProviders();
试试吧
@model ICollection< ApplicationUser>代替?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。