I need to create a contained user for my Azure SQL database using C#. I have the following code already for creating the database:
// Login to Azure
var credentials = UserTokenProvider.LoginSilentAsync(clientId, domainName, username, password).Result;
// Create client
SqlManagementClient client = new SqlManagementClient(credentials)
{
SubscriptionId = subscriptionId
};
// Database parameters
var databaseParameters = new Microsoft.Azure.Management.Sql.Models.Database()
{
Location = "ukwest",
ElasticPoolId = elasticPoolId,
};
// Create database
var dbResponse = client.Databases.CreateOrUpdate(resourceGroupName, serverName, databaseName, databaseParameters);
I have found the following SQL script that can be used to create a contained user using SQL management studio but this requires that you manually connect to the database that you want to create the user for:
CREATE USER [databaseUser] WITH PASSWORD = 'xxxxxxxxxxx';
ALTER ROLE [db_datareader] ADD MEMBER [databaseUser]
ALTER ROLE [db_datawriter] ADD MEMBER [databaseUser]
So how can I use the SQL script within my C# code to connect the database after it has been created to then create the contained SQL user for that database, or is there some equivalent code available in Microsoft.Azure.Management.Sql.SqlManagementClient for doing this?