I have an n-tiered solution that uses MVC-WebApi, Owin, ASP.NET Identity and EntityFramework. I used a sample identification code as a starter and added some custom properties to ApplicationUser. I also configured all classes to use Guids for Identity values. Everything is working fine so far.
I had difficulties when I wanted to transfer the IdentityConfig and IdentityModel files from the sample code from the MVC project and to the DataAccess project. These files define several key classes, such as ApplicationUserManager, ApplicationRoleManager, etc. ApplicationDbInitializer has code for getting ApplicationUserManager and ApplicationRoleManager using the current HttpContext to seed the database. It seems rather unnecessary to require the HttpContext to start the database. In addition, there are other background processes that I will add in the near future, when you will need to access Identity data from the business layer without the current HttpContext, so I want this dependency to be removed now.
I saw Q & A on this topic ASP authentication - Accessing the HttpContext in the reference library , but the best answer was pretty subtle. I tried to replace
var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
with
var userManager = new ApplicationUserManager(new ApplicationUserStore(db)); var roleManager = new ApplicationRoleManager(new RoleStore<IdentityRole, Guid, IdentityUserRole>(db));
but it REALLY led down the rabbit hole. The code does not compile unless I defined the user implementations of each Identity class and the implemented interfaces for them, etc. He needed all of the following (maybe more, I did not get it)
- ApplicationUserRole: IdentityUserRole
- ApplicationRole: IdentityRole, IRole
- ApplicationUserClaim: IdentityUserClaim
- ApplicationUserLogin: IdentityUserLogin
and IRole needs to be fully implemented, and I'm not sure how to do this.
I stopped at this point because it seems like there should be a simpler way or some example code that I can look at. I really don't need to configure anything except ApplicationUser. Am I going the right way here, or is my gut right and is there a better way?
source share