【问题标题】:How to remove all roles from user in ASP.NET Identity framework如何在 ASP.NET 身份框架中从用户中删除所有角色
【发布时间】:2020-04-11 22:42:06
【问题描述】:

我正在尝试使用下面的代码,但出现参数“manager”为空的异常。我该如何解决?

private ApplicationUserManager _userManager;

    [HttpDelete]
    public async Task DeleteUserRoles()
    {
        var userId = "ec6ea171-70f5-4fd9-93f2-5a05a2f88e3b";

        await _userManager.RemoveFromRolesAsync(userId, _userManager.GetRoles(userId).ToArray());

    }

【问题讨论】:

    标签: asp.net asp.net-identity


    【解决方案1】:

    有效的代码。

    using Microsoft.AspNet.Identity;
    using System.Threading.Tasks;
    using System.Web;
    using Microsoft.AspNet.Identity.Owin;
    
    .......
     private ApplicationUserManager _userManager; 
    
            public UsersController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
            {
                UserManager = userManager;
    
            }
            public ApplicationUserManager UserManager
            {
                get
                {
                   return _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
                }
                private set
                {
                    _userManager = value;
                }
            }
     [HttpDelete]
            public async Task DeleteUserRoles()
            {
                var userId = "ec6ea171-70f5-4fd9-93f2-5a05a2f88e3b";
                await UserManager.RemoveFromRolesAsync(userId, UserManager.GetRoles(userId).ToArray());
            }
    

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 2020-12-27
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多