title: Azure Active Directory B2C - Maintaining Users in the Database
body: <p>I'm transforming a legacy system that stores its users in a database (including credentials) to use Azure AD B2C for authentication.</p>
  
  <p>My first step is to rewrite the frontal API (the API that serves the web client directly)</p>
  
  <p>Because many other systems and database tables depend on the users table and its columns, I've decided on creating a db user for every new azure ad registration.</p>
  
  <p>This is the problem, the user id in the database is the primary key, an auto incremented number.</p>
  
  <p>The id that I extract from the access token claims is a ad object identifier, a GUID.</p>
  
  <p>To be able to relate an ad b2c user entity to a database user entity, I will have to create a new column in the users table, AzureObjectId.</p>
  
  <p>The problem is that now I would have to constantly do the conversion between AzureObjectId which I extract from the access token, to the database users id, because other database tables and other internal APIs that I access expect a database user id.</p>
  
  <p>What would be the right way to tackle this?<br>
  What I can thinks of is</p>
  
  <ol>
  <li>Transforming AzureObjectId to database user id during every interaction with the database or another internal API.</li>
  <li>Once my API creates the database user, use the Azure AD B2C Graph API to add a new claim, database user id, to the user.</li>
  </ol>
  
  <p>Both of these I want to avoid.
  Is there someway to enrich the access token with the database user id?</p>