【发布时间】:2015-06-24 11:04:16
【问题描述】:
我不想在我的域中引用 EntityFramework 和 Identity.EntityFramework 及其 IdentityUser。但我想使用Identity.Core 中的UserManager,它使用IUserStore<TUser>,其中TUser : IUser<string>。因此,我需要在隐藏ApplicationUser 的同时公开IUserStore,因为它源自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 不是变体(协方差)。
【问题讨论】:
-
理想情况下,您也不会公开 UserManager,因为它来自 AspNet 命名空间。
标签: asp.net-identity