ASP.NET Identity 2.0 UserManager.FindByIdAsyc未返回角色

ASP.NET Identity 2.0 UserManager.FindByIdAsyc未返回角色,asp.net,asp.net-mvc,entity-framework,asp.net-identity,roles,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Identity,Roles,我正在使用ASP.NET MVC v5.2、Entity Framework v6.0代码优先和Identity 2.0构建一个网站 我的UserAdmin控制器中有以下代码: // GET: /UserAdmin/Details/5 public async Task<ActionResult> Details(string id) { if (id == null) { return new Http

我正在使用ASP.NET MVC v5.2、Entity Framework v6.0代码优先和Identity 2.0构建一个网站

我的UserAdmin控制器中有以下代码:

    // GET: /UserAdmin/Details/5
    public async Task<ActionResult> Details(string id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var user = await UserManager.FindByIdAsync(id);
        return View(user);
    }
我的问题是,虽然我可以用UserManager填充用户的角色,但在使用FindByIdAsync方法时,它没有拾取与该用户关联的角色

以下是IdentityUserRole表中的数据,该表为分配给两个角色的突出显示用户显示:

以下是调试信息,显示与上述相同的用户,但角色计数为零:

为什么不返回此用户的角色

编辑1

我没有使用UserManager的默认实现

我的ApplicationUser扩展了IdentityUser以允许我添加自定义属性。我的ApplicationDbContext扩展了IdentityDbContext

这里是我使用Fluent API设置标识主键的地方:

        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
在扩展IdentityDbContext和IdentityUser时,不需要定义登录名、角色和用户角色的关系,因为它们已经在基类中定义。您需要删除modelBuilder.Entity.HasKeyr=>new{r.RoleId,r.UserId}这样的行;因为他们得到了照顾

至于不使用默认的UserManager,根据您提供的代码,这里有一种可能性

UserManager的默认实现将IUserStore作为其构造函数中的参数。如果您正在使用Microsoft.AspNet.Identity.EntityFramework库中的UserStore实现或从中派生,则对数据库的查询中将包括角色、声明和登录等内容。如果您正在创建自己的实现IUserStore的类,那么您需要自己包含相关数据。下面是一个例子

    public class ApplicationUser : IdentityUser
    {
        //Add your Custom Properties here..
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {

        //Add your Custom Properties here..
    }


    public class ApplicationUserStore : IUserStore<ApplicationUser>
    {
        private readonly ApplicationDbContext _context;

        public ApplicationUserStore(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<ApplicationUser> FindByIdAsync(string userName)
        {
            return await _context.Users.Include(x => x.Roles).FirstOrDefaultAsync(n => n.UserName == userName);
        }
    }
在扩展IdentityDbContext和IdentityUser时,不需要定义登录名、角色和用户角色的关系,因为它们已经在基类中定义。您需要删除modelBuilder.Entity.HasKeyr=>new{r.RoleId,r.UserId}这样的行;因为他们得到了照顾

至于不使用默认的UserManager,根据您提供的代码,这里有一种可能性

UserManager的默认实现将IUserStore作为其构造函数中的参数。如果您正在使用Microsoft.AspNet.Identity.EntityFramework库中的UserStore实现或从中派生,则对数据库的查询中将包括角色、声明和登录等内容。如果您正在创建自己的实现IUserStore的类,那么您需要自己包含相关数据。下面是一个例子

    public class ApplicationUser : IdentityUser
    {
        //Add your Custom Properties here..
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {

        //Add your Custom Properties here..
    }


    public class ApplicationUserStore : IUserStore<ApplicationUser>
    {
        private readonly ApplicationDbContext _context;

        public ApplicationUserStore(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<ApplicationUser> FindByIdAsync(string userName)
        {
            return await _context.Users.Include(x => x.Roles).FirstOrDefaultAsync(n => n.UserName == userName);
        }
    }

您是否使用UserManager、UserStore等的默认实现?a我可以看到您的IdentityUserRole有ApplicationUser_Id和IdentityRole_Id列。它们不应该在那里。调整DbContext以删除这些列。b当您从DbContext加载用户时,您需要专门加载角色-类似于此:@Excommunicated No,my ApplicationUser使用自定义属性扩展IdentityUser。我的ApplicationDBContext还扩展了IdentityDbContext。我正在通过Fluent API设置关系。@trailmax我不确定DbContext的哪个部分导致了这两个额外的列出现。我在我的帖子中添加了一些代码来展示我是如何使用Fluent API设置密钥的。IdentityUserRole是什么样子的?您是否使用UserManager、UserStore等的默认实现?a我可以看到您的IdentityUserRole有ApplicationUser_Id和IdentityRole_Id列。它们不应该在那里。调整DbContext以删除这些列。b当您从DbContext加载用户时,您需要专门加载角色-类似于此:@Excommunicated No,my ApplicationUser使用自定义属性扩展IdentityUser。我的ApplicationDBContext还扩展了IdentityDbContext。我正在通过Fluent API设置关系。@trailmax我不确定DbContext的哪个部分导致了这两个额外的列出现。我在我的帖子中添加了一些代码来展示我是如何使用Fluent API设置密钥的。IdentityUserRole看起来像什么?为这些好信息干杯。检查它,并将其标记为答案,如果它整理出来。除了删除Fluent API手动键设置,我还测试了调用base.OnModelCreatingmodelBuilder;在OnModelCreating中,为获得好信息干杯。检查它,并将其标记为答案,如果它整理出来。除了删除Fluent API手动键设置,我还测试了调用base.OnModelCreatingmodelBuilder;在OnModelCreating中,根据