【问题标题】:.net core IsInRoleAsync 'System.ObjectDisposedException'.net 核心 IsInRoleAsync 'System.ObjectDisposedException'
【发布时间】:2017-03-12 23:25:09
【问题描述】:

我正在使用 .net Core 1.1.1 处理应用程序 我需要检查当前用户是否是标签助手中的管理员角色成员。 TagHelper 的构造函数是

public MyTagHelper(UserManager<User> UserManager, IActionContextAccessor ActionContextAccessor)
    {
        userManager = UserManager;
        actionContextAccessor = ActionContextAccessor;            
    }

然后重写Process方法:

 public override async void Process(TagHelperContext context, TagHelperOutput output)
    {
     currentUser = await userManager.GetUserAsync(actionContextAccessor.ActionContext.HttpContext.User);
        isAdmin = await userManager.IsInRoleAsync(currentUser, "admin");
    }

如果保留字符串isAdmin = await userManager.IsInRoleAsync(currentUser, "admin") 未注释我有异常:“System.Private.CoreLib.ni.dll 中发生'System.ObjectDisposedException' 类型的未处理异常”

我不明白为什么。 感谢您的帮助。

【问题讨论】:

    标签: c# asp.net-core-mvc .net-core-1.1


    【解决方案1】:

    Process 是一种同步方法,将其设置为 async void 意味着它不会等待您的函数完成。您应该改写 ProcessAsync 并返回一个任务。试试这个:

    public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    {
        currentUser = await userManager.GetUserAsync(actionContextAccessor.ActionContext.HttpContext.User);
        isAdmin = await userManager.IsInRoleAsync(currentUser, "admin");
    }
    

    【讨论】:

    • 不客气,但在这里感谢某人的最佳方式是接受他们的回答并点赞。 (点击我答案左侧的绿色箭头和向上箭头)
    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 2018-06-30
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    相关资源
    最近更新 更多