问题描述
我不想引用EntityFramework,因此也不想引用Identity.EntityFramework
及其IdentityUser
在我的域中.但是我想使用Identity.Core
的UserManager
,其中使用IUserStore<TUser>
其中TUser : IUser<string>
.因此,我需要在隐藏ApplicationUser
的同时显示该IUserStore
,因为它是从IdentityUser
派生的.
I don't want to reference EntityFramework and hence Identity.EntityFramework
with its IdentityUser
in my domain. But I'd like to use UserManager
of Identity.Core
which uses IUserStore<TUser>
where TUser : IUser<string>
. And hence I need to expose that IUserStore
while hiding ApplicationUser
as it derives from 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不是变体(协方差),因此此代码不起作用.
This code does not work as TUser in IUserStore<TUser>
is not variant (covariance).
推荐答案
需要我编写从IUserStore
继承的Microsoft.AspNet.Identity.Framework.UserStore
适配器,因此可以与UserManager
一起使用.它将我的自定义用户对象映射到IdentityUser.其要点可以在 https://gist.github.com/w1ld中找到/b9228f5a27c54b061f90#file-userstoreadapter-cs 希望它对某人有帮助!
It required me to write an adapter to Microsoft.AspNet.Identity.Framework.UserStore
that inherits from IUserStore
and thus can be used with UserManager
. It maps my custom user object to IdentityUser. The gist for it can be found at https://gist.github.com/w1ld/b9228f5a27c54b061f90#file-userstoreadapter-cs Hope it helps someone!
这篇关于隐藏`IdentityUser`时,如何在我的业务层中显示`UserManager< IApplicationUser>`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!