How can we handle more than one user usage for identity core usermanagement and userstore without customization with generic type?

两盒软妹~` 提交于 2020-12-15 03:56:20

问题


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

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