다음을 통해 공유


코드에서 Azure SQL 데이터베이스에 액세스하는 애플리케이션을 인증하기 위한 필수 값 가져오기

적용 대상: Azure SQL 데이터베이스

코드에서 Azure SQL 데이터베이스를 만들고 관리하려면 Microsoft Entra ID(이전의 Azure Active Directory)로 앱을 등록해야 합니다. 앱은 Azure SQL 데이터베이스 리소스와 동일한 Microsoft Entra 테넌트에 등록되어야 합니다.

리소스에 액세스할 수 있는 서비스 주체 만들기

다음 예시는 AD(Active Directory) 응용 프로그램 및 C# 앱을 인증해야 하는 서비스 주체를 만듭니다. 스크립트는 이전 C# 샘플에 필요한 값을 출력합니다. 자세한 내용은 Azure PowerShell을 사용하여 리소스에 액세스하는 서비스 주체 만들기를 참조하세요.

중요

PowerShell Azure RM(Resource Manager) 모듈은 여전히 Azure SQL 데이터베이스에서 지원되지만 향후 모든 개발은 Az.Sql 모듈을 위한 것입니다. AzureRM 모듈은 적어도 2020년 12월까지 버그 수정을 계속 수신할 예정입니다. Az 모듈 및 AzureRm 모듈의 명령에 대한 인수는 실질적으로 동일합니다. 호환성에 대한 자세한 내용은 새로운 Azure PowerShell Az 모듈 소개를 참조하세요.

# 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

참고 항목

Azure SQL 데이터베이스에서 C#로 데이터베이스 만들기
Microsoft Entra 인증을 사용하여 SQL Database에 연결하기