4

I'm working on a web and mobile application for a taxi company and I use React.js and React-native for the front-end as well as ASP.NET Core for the back-end. I try to follow the onion architecture to build the web api but I have a problem for this part, it's about how to use in the best way ASP.NET Identity with this kind of architecture without going against the idea of the onion.

Like I need to authenticate and authorize the users for the access or not to a particular endpoint of my api, I thought of using Identity for doing this job. But I also need to have a User entity in my application domain to be able to link a customer or taximan to a specific taxi ride and make business logic with this entity. So, my problem is how to link Identity and my domain model without having a reference in my core project ?

I thought to use a mapper between the AppUser of Identity, who would be in its own infra project and the User core entity but I don't know if it's a good idea and even how to actually implement it.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • You can extend the IdentityUser, to have your needed Properties. This way you only need one User-Class. And if you need to have a public user, it could a Interface, that is implemented by your User-class. – Nikolaus Apr 07 '18 at 21:45
  • So I have a strong link with Identity in my core project ? –  Apr 08 '18 at 11:48
  • You don't have to, but you could. You also can use an IUser Interface, that has the Core Property of both, and cast it to the two concrete classes, when connecting to the database. – Nikolaus Apr 09 '18 at 06:14

2 Answers2

3

Check out this repository which implement a project with ASP.NET Identity with onion structure

https://github.com/imranbaloch/ASPNETIdentityWithOnion

Kahbazi
  • 14,331
  • 3
  • 45
  • 76
0

My understanding is that Identity.Core goes into your onion core. Other parts like Identity.EntityFramework might not be part of the Core but the User is part of your domain model. In any case this is what I do but I don't try to follow onion that strictly and tend to have pretty large core.

Stilgar
  • 22,354
  • 14
  • 64
  • 101
  • Yes, it's a solution but I don't want to have a `User` entity directly link to Identity. So if one day, I need to change Identity to an another system, it will not be a problem. –  Apr 08 '18 at 11:12
  • But that would be true for any identity system in the sense that you would at the very least couple by interface. You still need methods like those of the UserManager in your core so you will probably emulate them by interface if not by concrete type – Stilgar Apr 08 '18 at 15:38