2014-01-23 2 views
3

... 우리는 Microsoft.AspNet.Identity 밖에 없습니다. (우리는 Microsoft.AspNet.Identity.EntityFramework의 기본 구현도보고 있지 않습니다.)ASP.net Identity - UserManager는 어떻게 역할에 액세스합니까? <TUser>

UserManager 클래스는 생성자에서 IUserStore<TUser>만을 사용합니다. 그것은 UserManager.IsInRoleAsync(string, string) 여부를 결정하기 위해 액세스해야 할 것이라고 상상 한 IUserRoleStore<TUserRole>을 가지고 있지 않습니다.

UserStore의 구현에는 IsInRoleAsync(string, string) 기능도 포함될 것이라고 생각합니다.하지만 모두 이해할 수 있습니다. 속성으로 만 string Idstring UserName로 - 어떻게 UserManager은 그것의 구현 내부 알고 모두가 우리가 IUser 상대하고 있다는 것입니다 경우 암호 설정 및 재설정을 수행 할 수 있습니다 -

또 다른 이상한 것은

?

답변

5

좋아는, 이후 많은 발굴 및 운 발견 - 그것은 Microsoft.AspNet.Identity.Core는, 특히, IUserRoleStore<TUser>IUserPasswordStore<TUser>하는 모두 (시행) "상속"몇 가지 다른 인터페이스와 함께 제공 IUserStore 밝혀 <TUser>.

그래서 우리는 역할 관리 기능을 원한다면, 우리는 IUserRoleStore<TUser>을 구현 :

class MyUser : IUser 
{ 
     // Additional properties and functions not shown for brevity. 
} 

class MyUserStore : IUserRoleStore<MyUser> 
{ 
    public bool IsInRole(string username, string role) 
    { 
     // Implementation not show for brevity. 
    } 


    /* We would then implement the rest of the required functions. 

     We would have a data context here that has access to users, 
     user-roles, and roles. 
    */ 
} 

이제 우리는 UserManager<TUser>MyUserStore를 전달할 수 MyUserStoreIUserStore<TUser>IUserRoleStore<TUser>이기 때문에 :

UserManager<MyUser> UM = new UserManager<MyUser>(new MyUserStore()); 

I 그런 다음 UserManager<TUser>의 소스 코드가 리플렉션을 사용하여 저장소가 생성시 전달되었는지 여부를 확인합니다. 또는 역할 검사 (IUserRoleStore<TUser>을 구현하는 경우) 또는 암호 설정/재설정 (IUserPasswordStore<TUser>을 구현하는 경우)을 수행 할 수 있도록 IUserStore<TUserStore>의 "하위 인터페이스"중 ​​하나를 구현합니다.

대부분의 문서 (MVC 자습서 등)가이 정보를 제공하지 않기 때문에 유용하다고 생각합니다. Microsoft 012.AspNet.Identity.EntityFramework의 UserStore<TUser> 구현을 사용하라는 메시지가 표시됩니다. 사용자 정의 User 객체 (예 : IUser)를 전달하면됩니다.

+0

그것은 그것이 어떻게 작동하는지 excatly합니다. https://github.com/aspnet/Identity에서 소스 코드를 찾을 수 있습니다. 그들은'var cast = Store as IUserRoleStore ; '을 사용하여 당신의 전달 된 상점이'IUserRoleStore '을 구현하는지 확인합니다. – qbik

관련 문제