2017-02-07 1 views
0

유사를 IEnumerable/컬렉션 속성을 사용하여 사용자 ID를 연장 : How to extend available properties of User.Identity방법이 질문에

I 사용자가 로그인 내 사용자와 연관이있는 부서를로드하고자합니다. 나는이 같은 ApplicationUser 클래스에 속성을 추가 할 것 같은데요 :

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; } 
    } 

내 문제는 내 컨트롤러에서 나중에 속성에 액세스 할 어떻게 다음 컬렉션을 채우고 것 인 방법 /입니다. 내가 이해하는 바에 따르면, 클레임은 단순한 유형 (예 : ID)에서 사용할 수 있지만 컬렉션과 함께 사용할 수 있습니까?

일단이 속성이로드되면 사용자에 대한 정보가 필요할 때마다 데이터베이스를 치지 않고 컬렉션을 쿼리 할 수 ​​있다고 가정합니다.

도움을 주시면 감사하겠습니다.

+0

아마도 컬렉션을 저장하는 도메인 엔티티가 상기 ApplicationUser의 ID가 관계를 설정하는 참조 :

여기서,는 코드 1 엔티티 프레임 워크를 사용하는 가정은 예시입니까? – Esen

+0

ApplicationUser 클래스의 생성자에서? – daniel

답변

0

먼저 컬렉션 ​​개체, 즉 부서를 만듭니다. 그런 다음 ApplicationUser 엔터티의 ID를 참조하십시오.

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() 
    { 

    } 

} 
+0

Umut에 대한 많은 응답에 감사드립니다. Departments 컬렉션을 채우기 위해 DB를 사용할 위치를 찾지 못했습니다. – ETFairfax

+0

내가 지적했듯이 코드 우선 마이그레이션을 사용하는 경우 먼저 DbContext 클래스에 DbSet 형식의 속성으로 Department 엔터티를 추가해야합니다. 그런 다음 마이그레이션을 만들고 실행하십시오. 엔터티 프레임 워크를 사용하여 db 인스턴스에서 사용자의 인스턴스를 가져올 때마다 부서가 사용자를 위해 채워집니다. 관계 설정에 대한 자세한 내용은 [여기] (https://msdn.microsoft.com/en-us/library/jj713564(v=11113).aspx)를 참조하십시오. – Esen