问题
We have a multitenant architecture with more than one user like below with identity core library:
public class ApplicationUser : IdentityUser
{
}
public class Tenant1User: ApplicationUser
{
public int deneme { get; set; }
}
public class Tenant2User: ApplicationUser
{
public string City { get; set; }
public string Picture { get; set; }
public DateTime? BirthDay { get; set; }
public int Gender { get; set; }
}
We wanted to use UserManager generically in controllers constructors when injecting usermanager because we didn't want to use below tenant count could be getting bigger and controller will be full of usermanager injections :
private UserManager<Tenant1User> _userManager { get; }
private UserManager<Tenant2User> _userManager2 { get; }
public AccountController(
UserManager<Tenant1User> userManager,
UserManager<Tenant1User> userManager2,
) : base(logger: logger)
{
_userManager = userManager;
_userManager2 = userManager2;
}
We wanted to use like UserManager but this kind of implementation ended with errors and we didn't want to customize UserManager because default abilities are ok for us, we just want to handle for different type of users generically. How could we handle this kind of situation? Thank you.
回答1:
You could try to create a relation object to store the additional user information (instead of adding extension of the user: adding the custom field to the user table), then, configure relationship between them and the ApplicationUser table. Such as below:
public class ApplicationUser : IdentityUser
{
public virtual UserAdditionalInfo UserAdditionalInfo { get; set; }
}
/// used to add the additional information for users
public class UserAdditionalInfo
{
public int deneme { get; set; }
}
Then, in the controller, you could manage the user via the ApplicationUser table and UserManager() class:
private readonly UserManager<ApplicationUser> _userManager;
If you want to get the user additional information, try to load the related data or use the Join operator.
来源:https://stackoverflow.com/questions/64964545/how-can-we-handle-more-than-one-user-usage-for-identity-core-usermanagement-and