【问题标题】:Role Manager UI ASP.NET CORE Identity & EF角色管理器 UI ASP.NET CORE Identity & EF
【发布时间】:2018-12-27 11:18:13
【问题描述】:

我创建了一个小型车队管理应用程序,并借助我在 Microsoft 网站和其他网站上找到的教程和材料。

我设法实现了用户管理服务(从一开始就没有使用身份验证创建项目),现在可以创建新用户并从界面管理他们,尽管我创建了一个“管理员”角色并将其分配给管理员帐户(通过 Startup.cs),我很难创建一个 UI 来管理角色(创建、修改、删除、为用户分配角色)。我已经搜索了 3 天,但找不到关于如何使用 RoleManager 为其创建控制器和视图的合适教程。

谁能指出我正确的方向?

我正在使用 ASP.NECore.All 2.0.6 和 EF Core 2.1.1

AccountController:

 public class AccountController : Controller
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly SignInManager<ApplicationUser> _signInManager;
    private readonly IEmailSender _emailSender;
    private readonly ILogger _logger;

    public AccountController(
        UserManager<ApplicationUser> userManager,
        SignInManager<ApplicationUser> signInManager,
        IEmailSender emailSender,
        ILogger<AccountController> logger)
    {
        _userManager = userManager;
        _signInManager = signInManager;
        _emailSender = emailSender;
        _logger = logger;
    }

管理控制器:

    public class ManageController : Controller
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly SignInManager<ApplicationUser> _signInManager;
    private readonly IEmailSender _emailSender;
    private readonly ILogger _logger;
    private readonly UrlEncoder _urlEncoder;

    private const string AuthenticatorUriFormat = "otpauth://totp/{0}:{1}?secret={2}&issuer={0}&digits=6";
    private const string RecoveryCodesKey = nameof(RecoveryCodesKey);

    public ManageController(
      UserManager<ApplicationUser> userManager,
      SignInManager<ApplicationUser> signInManager,
      IEmailSender emailSender,
      ILogger<ManageController> logger,
      UrlEncoder urlEncoder)
    {
        _userManager = userManager;
        _signInManager = signInManager;
        _emailSender = emailSender;
        _logger = logger;
        _urlEncoder = urlEncoder;
    }

其余操作是默认操作,您在使用个人帐户身份验证创建新项目时获得这些操作。

【问题讨论】:

    标签: c# model-view-controller asp.net-core-2.0 identity role-manager


    【解决方案1】:

    您可以使用由 asp.net 核心身份实现提供的 IdentityRole 类。首先,将角色管理器添加到您的类并注入它。

    private readonly RoleManager&lt;IdentityRole&gt; _roleManager;

    在您的构造函数中传递RoleManager&lt;IdentityRole&gt; 并添加

    _rolemanager = rolemanager

    要创建新角色,请在视图和控制器中使用 IdentityRole

    public IActionResult AddRole()
    {
        var role = new IdentityRole
        {
            Name = "Customer",  //Name will be sufficient
        };
        _roleManager.CreateAsync(role); //role actually comes as parameter from view
        return View();
    }
    

    在您的用户创建和更新视图中,您可以读取所有角色并创建一个下拉列表(使用多选来分配多个角色)。

    public IList<IdentityRole> GetAllRoles()
    {
        return _roleManager.Roles.ToList();
    }
    

    最后,在添加/更新用户时,您现在应该将它们分配给角色。

    //single
    _userManager.AddToRoleAsync(user, role);  //role should be role name
    
    //multiple
    _userManager.AddToRolesAsync(user, roles); // roles should be list of role names
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 2021-09-19
      • 2023-04-06
      • 2012-08-24
      • 2020-07-21
      • 2014-10-28
      相关资源
      最近更新 更多