我不想引用 EntityFramework 因此Identity.EntityFramework
以其IdentityUser
在我的域中。但我想用UserManager
of Identity.Core
它使用IUserStore<TUser>
where 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>();
此代码不能作为 TUser 使用IUserStore<TUser>
不是变体(协方差)。
它需要我写一个适配器Microsoft.AspNet.Identity.Framework.UserStore
继承自IUserStore
因此可以与UserManager
。它将我的自定义用户对象映射到 IdentityUser。其要点可以在以下位置找到:https://gist.github.com/w1ld/b9228f5a27c54b061f90#file-userstoreadapter-cs希望它能帮助别人!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)