Red*_*dge 5 asp.net asp.net-authorization .net-core
也许我错过了一些东西,但是,我阅读了大量关于使用 .NET Core 2.0 进行身份验证和授权的文档和文章,但我没有找到任何关于用户管理的内容。
我想要实现的是拥有一个管理员用户界面,可以创建用户、列出所有现有用户并将他们分配给预定义的角色和/或预定义的策略。
我尝试这样做但没有成功(我在尝试使用模型时遇到问题,例如IEnumerable<IdentityUser>关于无效构造函数:
InvalidOperationException:找不到适合类型“System.Collections.Generic.IEnumerable`1[Microsoft.AspNetCore.Identity.IdentityUser]”的构造函数。确保类型是具体的,并且为公共构造函数的所有参数注册了服务。
我无法RoleManager在任何控制器中获取。它适用于 UserManager,但不适用于 RoleManager。我加了
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)
到启动,所以我教它会自动注入 DI ......
ApplicationUser 定义如下:
namespace KaraokeServices.Data
{
public class ApplicationUser : IdentityUser
{
}
}
Run Code Online (Sandbox Code Playgroud)
我定义了一个 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是 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>
Run Code Online (Sandbox Code Playgroud)
但我总是被错误所困扰......
我做错了什么?
代替
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)
将管理人员添加到
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<AuthContext>()
.AddUserManager<UserManager>()
.AddSignInManager<SignInManager>()
.AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)
并尝试与
@model ICollection<ApplicationUser>反而?
| 归档时间: |
|
| 查看次数: |
2040 次 |
| 最近记录: |