本文介绍了隐藏`IdentityUser`时,如何在我的业务层中显示`UserManager< IApplicationUser>`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想引用EntityFramework,因此也不想引用Identity.EntityFramework及其IdentityUser在我的域中.但是我想使用Identity.CoreUserManager,其中使用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&lt; IApplicationUser&gt;`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:14