Share via


SAS 정의를 만들고 코드에서 공유 액세스 서명 토큰 가져오기(레거시)

키 자격 증명 모음에 저장된 SAS(공유 액세스 서명) 토큰을 사용하여 스토리지 계정을 관리할 수 있습니다. 자세한 내용은 SAS를 사용하여 Azure Storage 리소스에 대한 제한된 액세스 권한 부여를 참조하세요.

참고 항목

Azure RBAC(Azure 역할 기반 액세스 제어)를 사용하여 공유 키 권한 부여에 대한 뛰어난 보안 및 사용 편의성을 위해 스토리지 계정을 보호하는 것이 좋습니다.

이 문서에서는 SAS 정의를 만들고 SAS 토큰을 가져오는 .NET 코드 샘플을 제공합니다. Key Vault 관리형 스토리지 계정에 대해 생성된 클라이언트를 포함한 자세한 내용은 ShareLink 샘플을 참조하세요. SAS 토큰을 만들고 저장하는 방법에 대한 자세한 내용은 Key Vault 및 Azure CLI를 사용하여 스토리지 계정 키 관리 또는 Key Vault 및 Azure PowerShell을 사용하여 스토리지 계정 키 관리를 참조하세요.

코드 샘플

다음 예제에서는 SAS 템플릿을 만듭니다.

private static string BuildSasDefinitionTemplate(bool readOnly) =>
    new StringBuilder("sv=2018-03-28")  // service version
        .Append("&spr=https")           // HTTPS only
        .Append("&ss=bf")               // blobs and files only
        .Append("&srt=o")               // applies to objects only
        .Append(readOnly ? "&sp=r" : "&sp=rw")  // read-only or read-write
        .ToString();

이 템플릿을 사용하면 SAS 정의를 만들 수 있습니다.

string sasDefinitionName = BuildSasDefinitionName(Tag, readOnly, duration);
SasDefinitionAttributes sasDefinitionAttributes = new SasDefinitionAttributes
{
    Enabled = true,
};

Dictionary<string, string> tags = new Dictionary<string, string>
{
    [Tag] = "1",
};

SasDefinitionBundle createdSasDefinition = await storageClient.SetSasDefinitionAsync(
    storageAccountName,
    sasDefinitionName,
    sasTemplate,
    SasTokenType.Account,
    duration,
    sasDefinitionAttributes,
    tags,
    s_cancellationTokenSource.Token);

SAS 정의가 만들어지면 SecretClient를 사용하여 비밀과 같은 SAS 토큰을 검색할 수 있습니다. 비밀 이름은 스토리지 계정 이름 뒤에 대시를 붙여 시작해야 합니다.

// Build our SAS template, get an existing SAS definition, or create a new one.
string sasTemplate = BuildSasDefinitionTemplate(readOnly);
string sasDefinitionName = await GetOrCreateSasDefinitionAsync(storageClient, storageAccountName, sasTemplate, days, readOnly);

// Now we can create a SecretClient and generate a new SAS token from the storage account and SAS definition names.
SecretClient secretClient = new SecretClient(vaultUri, credential, options);
KeyVaultSecret sasToken = await secretClient.GetSecretAsync($"{storageAccountName}-{sasDefinitionName}", cancellationToken: s_cancellationTokenSource.Token);

공유 액세스 서명 토큰이 곧 만료되는 경우 동일한 비밀을 다시 가져와서 새 토큰을 생성할 수 있습니다.

Key Vault SAS 토큰에서 검색을 사용하여 Azure Storage 서비스에 액세스하는 방법에 대한 가이드는 계정 SAS를 사용하여 Blob 서비스에 액세스를 참조하세요.

참고 항목

앱은 Storage에서 403 오류가 발생할 경우 키가 손상되어 일반 회전 기간보다 더 빠르게 회전시켜야 하는 상황에 대비할 수 있도록 SAS를 새로 고칠 준비가 되어 있어야 합니다.

다음 단계