This is more a question of async / wait than ASP.NET Identity. I am using Identity Asp.Net and have a custom UserStore with a custom GetRolesAsync method. UserManager is called from the WebApi controller.
public class MyWebApiController {
private MyUserManager manager = new MyUserManager(new MyUserStore());
[HttpGet]
public async Task<bool> MyWebApiMethod(int x) {
IList<string> roles = await manager.GetRolesAsync(x);
return true;
}
}
public class MyUserManager : UserManager<MyUser, int> {
}
public class MyUserStore {
public async Task<IList<string>> GetRolesAsync(TUser user) {
var currentContext = System.Web.HttpContext.Current;
var query = from userRole in _userRoles
where userRole.UserId.Equals(userId)
join role in _roleStore.DbEntitySet on userRole.RoleId equals role.Id
select role.Name;
return await query.ToListAsync();
}
}
Why is the context null in MyUserStore.GetRolesAsync? I thought, what awaits when the context passes? I went through the other async methods in MyUserStore and they all have the right context and the code seems almost identical.
source
share