Delen via


De vereiste waarden ophalen voor het verifiëren van een toepassing voor toegang tot Azure SQL Database vanuit code

van toepassing op:Azure SQL DatabaseSQL-database in Fabric

Als u een Azure SQL Database wilt maken en beheren op basis van code, moet u uw app registreren bij Microsoft Entra ID (voorheen Azure Active Directory). De app moet zijn geregistreerd in dezelfde Microsoft Entra-tenant als uw Azure SQL Database-resource.

Een service-principal maken voor toegang tot resources vanuit een toepassing

In de volgende voorbeelden worden de Microsoft Entra-toepassing en de service-principal gemaakt die we nodig hebben om onze C#-app te authentiseren. Het script voert waarden uit die we nodig hebben voor het voorgaande C#-voorbeeld. Zie Azure PowerShell gebruiken om een service-principal te maken voor toegang tot resourcesvoor gedetailleerde informatie.

Belangrijk

De Module PowerShell Azure Resource Manager (AzureRM) is afgeschaft op 29 februari 2024. Voor alle toekomstige ontwikkeling moet de Az.Sql-module worden gebruikt. Gebruikers wordt aangeraden om van AzureRM naar de Az PowerShell-module te migreren om ondersteuning en updates te garanderen. De AzureRM-module wordt niet meer onderhouden of ondersteund. De argumenten voor de opdrachten in de Az PowerShell-module en in de AzureRM-modules zijn aanzienlijk identiek. Zie Introductie van de nieuwe Az PowerShell-modulevoor meer informatie over de compatibiliteit.

# sign in to Azure
Connect-AzAccount

# for multiple subscriptions, uncomment and set to the subscription you want to work with
#$subscriptionId = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"
#Set-AzContext -SubscriptionId $subscriptionId

$appName = "{app-name}" # display name for your app, must be unique in your directory
$uri = "http://{app-name}" # does not need to be a real uri
$secret = "{app-password}"

# create an AAD app
$azureAdApplication = New-AzADApplication -DisplayName $appName -HomePage $Uri -IdentifierUris $Uri -Password $secret

# create a Service Principal for the app
$svcprincipal = New-AzADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId

Start-Sleep -s 15 # to avoid a PrincipalNotFound error, pause here for 15 seconds

# if you still get a PrincipalNotFound error, then rerun the following until successful.
$roleassignment = New-AzRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $azureAdApplication.ApplicationId.Guid

# output the values we need for our C# application to successfully authenticate
Write-Output "Copy these values into the C# sample app"

Write-Output "_subscriptionId:" (Get-AzContext).Subscription.SubscriptionId
Write-Output "_tenantId:" (Get-AzContext).Tenant.TenantId
Write-Output "_applicationId:" $azureAdApplication.ApplicationId.Guid
Write-Output "_applicationSecret:" $secret

Zie ook

Een database maken in Azure SQL Database met C#
Verbinding maken met Azure SQL Database met behulp van Microsoft Entra-verificatie