I have an application that uses identity database to store users and customers.
Each customer has also a separate database with its data and its connection string is stored in Customer table in the identity database.
AspNetUsers has a field to tell which customer the user belongs to (also identity db).
I want to assign connection string to the user when he logs in and make it available in the application for the duration of the session.
I currently have customer model:
public partial class `Customer`
{
public int Id { get; set; }
public string Name { get; set; }
public int NoLicenses { get; set; }
public bool? Enabled { get; set; }
public string CustomerConnectionString { get; set; }
}
and user model:
public class ApplicationUser : IdentityUser
{
public string CustomerId { get; set; }
public bool? IsEnabled { get; set; }
// there ideally I'd have a connstring property
}
The models map db table fields.
I'm using .NET Core 1.1 and EF Core.
With the defalut ASP.NET Identity template , you can :
extend the ApplicationUser class in
Modelsfolder :Add your custom model to
ApplicationDbContextin Data folder :Sync your database : Add-Migration xxxx , then run the Update-Database command in Package Manager Console . Now you have the Customer table and have
CustomerConnectionStringcolumn inAspNetUserstable.Create you own implementation of IUserClaimsPrincipalFactory by inheriting the default one to generate a ClaimsPrincipal from your user :
Register the custom factory you just created in your application startup class, after adding Identity service:
Then you could access the claims like :
Modify the creating/editing user view/controller , add the customer dropdownlist on view , get the custom id in
Registerfunction inAccountController, query the connectingString of custom from db , and save inApplicationUserobject :