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
.
In my data access layer:
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);
In my domain layer:
public interface IApplicationUser : IUser<string> {}
// desired behavior somewhere in domain/web:
var myUserManager = iocContainer.Resolve<UserManager<IApplicationUser>();
This code does not work as TUser in IUserStore<TUser>
is not variant (covariance).
It required me to write an adapter to
Microsoft.AspNet.Identity.Framework.UserStore
that inherits fromIUserStore
and thus can be used withUserManager
. 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!