We are migrating from on-prem database into Azure SQL. We have audit fields in the database that grabs the SUSER_NAME()
whenever a DML transaction (i.e., INSERT, UPDATE, DELETE - soft-delete) occurs. Since the on-prem database uses the Windows Domain Active Directory and the users login to the database using Integrated Security we can correctly get the current user's name.
However, when we use the managed identity in Azure SQL, the SUSER_NAME()
comes down as a concatenation of two guids
: the Managed Identity guid
and the service container guid
. As a result, we are losing the user name of the current user. Is it possible to get the user name with the SQL Function SUSER_NAME()
(or some other SQL function) or will this be a code change where we now pass in the actual user name, for the audit field in question, to the call to the Azure SQL database?
Update 1:
Our application is designed to interact with Azure SQL using the following DbContext
constructor:
public SomeDbEntities(IConfiguration objConfiguration, AzureAuthenticationInterceptor azureAuthenticationInterceptor) :
base(new DbContextOptionsBuilder<SomeDbEntities>()
.UseSqlServer(objConfiguration.GetValue<string>("Azure:ConnectionStrings:SomeDbEntities"))
.AddInterceptors(azureAuthenticationInterceptor)
.Options)
{ }
We are using an azureAuthenticationInterceptor
in order to deal with a thread blocking issue. The solution for that was provided here: EF Core Connection to Azure SQL with Managed Identity
Update 2:
The audit fields are being set by triggers
using the SUSER_NAME()
function that log data to a history table.