2017-03-12 204 views
0

我在使用.net Core 1.1.1登录应用程序 我需要检查当前用户是否是标记助手中的管理员角色的成员。该TagHelper的构造是.net核心IsInRoleAsync'System.ObjectDisposedException'

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

然后覆盖加工方法:

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.ObjectDisposedException’发生在系统的未处理的异常。 Private.CoreLib.ni.dll“

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

回答

0

进程是一个同步方法,并使其async void意味着它不会等待您的函数完成。您应该改写ProcessAsync并返回一个Task。试试这个:

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

谢谢。有用。 – gikerix

+0

不客气,但感谢这里的人最好的方式就是接受他们的回答,并加以满足。 (点击我答案左边的绿色箭头和向上箭头) –