ASP Core. Best practices for multiple identities

人走茶凉 提交于 2021-02-08 04:32:53

问题


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?


回答1:


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.


来源:https://stackoverflow.com/questions/53227258/asp-core-best-practices-for-multiple-identities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!