0

Similar to this question: How to extend available properties of User.Identity

When a user logs in I'd like to load the departments my user has an association with. I'm guessing that I'd add a property to the ApplicationUser class like this:

public class ApplicationUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            return userIdentity;
        }


        public IEnumerable<Department> Departments { get; set; }
    }

My question is how/where would I populate the collection, and then how would I access the property later in my controllers. From what I understand, claims would be OK for simple types - an Id for example - but can they be used with collections?

I'm assuming once I have this property loaded, I'd be able to query the collection without hitting the database each time I need this information about the user - which will be often.

Any help appreciated.

Community
  • 1
  • 1
ETFairfax
  • 3,794
  • 8
  • 40
  • 58

1 Answers1

0

First of all, create the collection entity, i.e. Department. Then reference the Id of the ApplicationUser entity within that.

Assuming you use entity framework code-first, here is an example:

public class ApplicationUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            return userIdentity;
        }

        public ApplicationUser()
        {
           Departments = new Collection<Department>();
        }

        public ICollection<Department> Departments { get; set; }
    }



public class Department
{

    public string UserId { get; set; }

    public int DepartmentId { get; set; }

    public ApplicationUser User { get; set; }

    protected Department()
    {

    }

}
umutesen
  • 2,523
  • 1
  • 27
  • 41
  • Many thanks for the response Umut. I don't get where you would hit the DB to populate the Departments collection. – ETFairfax Feb 07 '17 at 11:38
  • As I pointed out, if you are using code-first migrations, you would first add your Department entity into the DbContext class as a property of type DbSet. Then create & run a migration. Whenever you get an instance of a user from the db with entity framework, the departments will be populated for your. for more information on setting up relationships please take a look [here](https://msdn.microsoft.com/en-us/library/jj713564(v=vs.113).aspx) – umutesen Feb 07 '17 at 14:10