ovasylenko
ovasylenko

Reputation: 2775

ASP Core. Best practices for multiple identities

env: Asp Core, Entity-framework

In my system I have two types

[Table("User")]
ApplicationUser : IdentityUser<Guid>

[Table("Customer")]
Customer : IdentityUser<Guid>

Both entities(Customer and User) have many different fields, what makes using only one table in a Database not correct. And both entities must have possibility to do sign in.
As I found ASP Net can have only one identity setup.

Question: What is the best way or best practices to make this stuff work?

Upvotes: 2

Views: 1257

Answers (1)

Rustam Ashurov
Rustam Ashurov

Reputation: 186

I suggest you not try to mix or unify Identity of application user and meanings of application user from another boundary contexts of you application.

Identity record of application user is his representation from security perspective, and it is used for identification/authentication of user. So it contains user security data, its access roles and other security claims. Any Identity record can have very specific access right based on his roles and claims and usually it is enough.

If you need to represent application user from another perspective (as your employ, or as you customer, maybe as guest record, etc.) then it is better to create another table (Employees, Customers, Guests , etc.) for it in another DbContext (not in Identity context). It will give you possibility not to mix their conceptual borders. Who knows, maybe at some moment you will decide to create separated microservices for each boundary context and Identity will serve them all as another microservice.

If you asking now yourself how to organize such parallel storing of interpretations of the same application user, then there are different approaches. But for example:

  • When user is registering you create Identity for him
  • When he is logged-in he use his Identity data for authentication
  • But when he create his first order you create Customer record for him that has same Id as his Identity, or has foreign key to Identity, or... the rest depends on your needs and business logic.

Upvotes: 4

Related Questions