如何在隐藏 IdentityUser 的同时将 UserManager<IApplicationUser> 暴露给我的业务层?

How do I expose `UserManager<IApplicationUser>` to my business layer while hiding `IdentityUser`?

我不想在我的域中引用 EntityFramework,因此 Identity.EntityFramework 及其 IdentityUser。但我想使用 Identity.CoreUserManager,它使用 IUserStore<TUser>,其中 TUser : IUser<string>。因此,我需要公开 IUserStore,同时隐藏 ApplicationUser,因为它源自 IdentityUser

在我的数据访问层中:

public class ApplicationUser : IdentityUser, IApplicationUser { }

// somewhere for IoC container:
var userStore = new UserStore<ApplicationUser>();
// The following breaks with error CS0266: 
//     Cannot implicitly convert type 'UserStore<ApplicationUser>' to 'IUserStore<IApplicationUser>'
IUserStore<IApplicationUser> userStore = userStore;  // does not work
var um = new UserManager<IApplicationUser>(userStore);

在我的领域层:

public interface IApplicationUser : IUser<string> {}

// desired behavior somewhere in domain/web:
var myUserManager = iocContainer.Resolve<UserManager<IApplicationUser>();

此代码不起作用,因为 IUserStore<TUser> 中的 TUser 不是变体(协方差)。

它要求我为 Microsoft.AspNet.Identity.Framework.UserStore 编写一个继承自 IUserStore 的适配器,因此可以与 UserManager 一起使用。它将我的自定义用户对象映射到 IdentityUser。可以在 https://gist.github.com/w1ld/b9228f5a27c54b061f90#file-userstoreadapter-cs 找到它的要点希望它对某人有所帮助!