The following will work from any controller.
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
...
var users = Request.GetOwinContext().GetUserManager
The generated controllers have a line like this:
private ApplicationDbContext db = new ApplicationDbContext();
After the above initialization takes place, you can also get a list of users by simply doing:
db.Users
Notice, I am NOT using db.ApplicationUsers which you may have been tempted to add to the ApplicationDbContext class using the following:
public DbSet
The problem as noted here is that that IdentityDbContext already has the following defined.
public virtual IDbSet
So, by adding the ApplicationUsers line above you have added two properties that map to the ApplicationUsers. Here is the error you will get if you have done this:
Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'MyApp.Models.ApplicationUser'.
No comments:
Post a Comment