1
$\begingroup$

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).

$\endgroup$
1
  • $\begingroup$ Ideally you wouldn't expose UserManager either, as that is coming from an AspNet namespace. $\endgroup$ Commented Jun 24, 2015 at 17:51

1 Answer 1

0
$\begingroup$

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!

$\endgroup$
Sign up to request clarification or add additional context in comments.

6 Comments

You seem to implement your own ApplicationUserStore - you called it UserStoreAdapter. I am still trying to implement a UserManager with ApplicationUser like the Asp.Net Webapplikation Template does, without implementing everything myself... but i am not there yet :(
Implementing a new UserManager should go smooth. Not sure I understand the difficulty. The problem I had is I didn't want to reference Identity.EntityFramework in my project with UserManager.
I am trying something pretty similar. I want to separate IdentityUser (ApplicationUser) into a different project (Entities). Another project should hold the repository and another the WebApi project. This should later make testing easier. If you got a few minutes I am trying this out here: github.com/ddNils/TestWebApi02
@Nils What's required? 1) The point is if your tests should run on database or not. Means if you can go along with EF. I didn't take this way and made it without EF. So I abstracted out UserStore using the adapter. Your code now depends on EF: the IdenityUser. 2) UserManager is more like a business logic and can be independent of UserStore and hence from EF. Your version is in Repo which is strange. 3) So to make UserManager with ApplicationUser inherit it from IUser if without EF way and remove all EF references. If with EF then I don't see point why not use UserStore.
For me it felt like, when inheriting from Identity - i did get all properties (like UserName or Email). But when inheriting from IUser I had to write all my properties myself. My goal is to not invent the wheel again: Why should I code all user properties, when i only want to add one more?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.