使用ASP.NET Core 2.0进行用户管理
也许我想念一些东西但是,我阅读了大量关于使用.NET Core 2.0进行身份验证和授权的文档和文章,但我没有找到任何关于用户管理的内容.
我想要实现的是拥有一个管理员用户界面,可以创建用户,列出所有现有用户并将其分配给预定义角色和/或预定义策略. 我没有成功就试着没有运气(尝试使用IEnumerable< IdentityUser>等模型时遇到无效的构造函数时遇到问题:
而且我无法在任何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>代替? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 我的C#Web应用程序项目中缺少IdentityConfig.cs
- asp.net-mvc-4 – URL.Action参数中的客户端值
- asp.net – HttpCookie.Expire时区含义?
- asp.net-mvc – Asp.Net MVC – “动态”路由的最佳方法
- asp.net-mvc – 服务txt的asp.net mvc被截断
- 如何在ASP.NET中使用log4net在日志文件中包含SessionID?
- asp.net – 如何从另一个程序集中提供内容
- asp.net-mvc – Ajax.BeginForm和验证
- asp.net – 在asp:GridView中插入隐藏列但仍然可用的客户端
- asp.net – 母版页和嵌套表单问题
- asp.net – 使用ASP MVC 3 RC和.NET 4中的Reques
- asp.net-mvc-4 – ASP.NET MVC 4网站速度问题
- asp.net – 在根站点下托管的应用程序中配置子目
- asp.net-mvc-3 – 如何建模绑定实现接口的类?
- asp.net-mvc – ‘System.Web.Webpages.Html.Htm
- 动态加载asp.net网站中的用户控件(ascx)
- asp.net – ASP .NET MVC中的TinyMCE Spellcheck
- 使用ASP.NET重新发明我的身份验证策略
- ASP.NET – meta:ResourceKey vs <%$Resources
- asp.net-mvc – HttpPostedFileBase总是在ASP.NE