asp.net - 如何在 ASP.NET 身份框架中从用户中删除所有角色
问题描述
我正在尝试使用下面的代码,但得到参数“manager”为空的异常。我该如何解决?
private ApplicationUserManager _userManager;
[HttpDelete]
public async Task DeleteUserRoles()
{
var userId = "ec6ea171-70f5-4fd9-93f2-5a05a2f88e3b";
await _userManager.RemoveFromRolesAsync(userId, _userManager.GetRoles(userId).ToArray());
}
解决方案
有效的代码。
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());
}
推荐阅读
- timestamp - UTC时间戳需要保存在java变量中
- php - 从 ZendFramework 3 调用存储过程
- javascript - react-native 将两个数组合二为一
- node.js - 使用 sinon 模拟连接池和测试 url 参数?
- javascript - 将javascript字符串转换为数组
- java - SpringBoot 2.0.4.RELEASE 中的 ResponseEntityExceptionHandler
- c# - 在 C# 中具有 IF 条件的睡眠线程
- vba - VBScript 允许在 Excel 文件中编辑并另存为
- android - Android:从左侧拉动 SwipeRefreshLayout 进行刷新
- javascript - 在chartjs的条形顶部添加图像