1

I trying something quite simple but for whatever reason it does not work, and for that reason I here to see if something gives me any light, and I could not find examples or solutions somewhere else (Bing/google)

The problem is quite simple I am using a slightly modified ApplicationUser class:

 public class ApplicationUser : IdentityUser
{
    public virtual Guid BusinessId { get; set; }
}

With the following helper (Extension method):

public static Guid GetBusinessId(this IIdentity user)
    {
         var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
         var currentUser = manager.FindById(user.GetUserId());

        return currentUser.BusinessId;
    }

Then finally the method that is "causing" the issue:

public IQueryable<ApplicationUser> Get()
    {
        var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

        Guid businessId = this.User.Identity.GetBusinessId();
        var users = userManager.Users.Where(u => u.BusinessId == businessId);

        return users;
    }

What happens here is that when I query Users, I get nothing but it should at least get the current logged in User, since he has the proper businessId.

If I put a break point on the users query and try anything like "ToList()" or "ToList()[0]" on the immediate window I get the following:

?users.ToList()
Count = 1
[0]: Could not evaluate expression
?users.ToList()[0]
An internal error has occurred while evaluating method System.Collections.Generic.List`1[T].get_Item().

Any ideas why, or is there a "right" way of doing this?

Thanks

Oakcool
  • 1,470
  • 1
  • 15
  • 33

1 Answers1

1

have a look here How to obtain a list of Users from ASP.NET Identity? qouted from answer

I found out that I wasn't using the derived ApplicationUser object for anything, so I just went ahead and changed all uses of it for plain old User. Then I just changed ApplicationDbContext definition for the following:

public class ApplicationDbContext : IdentityDbContext<
    User, UserClaim, UserSecret, UserLogin,
    Role, UserRole, Token, UserManagement>
{
}

And now I can access the user list:

 UsersContext = new ApplicationDbContext();
 ...
 UsersContext.Users.ToList();

However, I think this will come back and haunt me in the future (I'll probably need to add more fields to User) so probably I'll have to use the same approach as in this question:

Get all role names in ASP.NET MVC5 Identity system

Edit: Since I got the need to add a new property, I had to revert my changes. So I went ahead and did a line by line comparison with the ASP.NET Identity Sample Project, and found out that the generated project had the following line:

IdentityManager = new AuthenticationIdentityManager(new IdentityStore());

while the Sample application had included the database context in the constructor. So I added it in my constructor, recreated the database and the problem went away.

Community
  • 1
  • 1
VisualBean
  • 4,908
  • 2
  • 28
  • 57
  • I could not find the IdentityManager nor AuthenticationIdentityManager that he talks about, and the first example does not work since I have a modification on the ApplicationUser – Oakcool Apr 29 '14 at 07:10