さて、多くの掘り下げと幸運な発見の後、特に 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.
*/
}
これで、 に渡すことができます。MyUserStore
これはであるからです。UserManager<TUser>
MyUserStore
IUserRoleStore<TUser>
IUserStore<TUser>
UserManager<MyUser> UM = new UserManager<MyUser>(new MyUserStore());
したがって、 のソース コードは、UserManager<TUser>
リフレクションを使用して、コンストラクターで渡されたストアが の「子インターフェイス」の 1 つを実装しているかどうかを調べて、ロール チェック (実装されている場合) またはパスワード セットIUserStore<TUserStore>
を実行できるようになっていると思われます。 IUserRoleStore<TUser>
/reset (実装している場合IUserPasswordStore<TUser>
)。
ほとんどのドキュメント (MVC チュートリアルなど) ではこの詳細が説明されていないため、これがお役に立てば幸いです。彼らはUserStore<TUser>
、Microsoft.AspNet.Identity.EntityFramework の実装を使用するように指示しています。ここでは、( を実装する) カスタムUser
オブジェクトを渡すだけでIUser
よいのです。