Delen via


Create an account SAS with .NET

Met een Shared Access Signature (SAS) kunt u beperkte toegang verlenen tot containers en blobs in uw opslagaccount. Wanneer u een SAS maakt, geeft u de beperkingen op, waaronder welke Azure Storage-resources een client mag openen, welke machtigingen ze hebben voor deze resources en hoe lang de SAS geldig is.

Elke SAS is ondertekend met een sleutel. U kunt een SAS op twee manieren ondertekenen:

  • With a key created using Microsoft Entra credentials. A SAS that is signed with Microsoft Entra credentials is a user delegation SAS. Aan een client die een SAS voor gebruikersdelegering maakt, moet een Azure RBAC-rol worden toegewezen die de actie Microsoft.Storage/storageAccounts/blobServices/generateUserDelegationKey bevat. Voor meer informatie, zie Een SAS voor gebruikersdelegatie maken.
  • Met de sleutel van het opslagaccount. Zowel een service-SAS als een account-SAS zijn ondertekend met de sleutel van het opslagaccount. De client die een service-SAS maakt, moet directe toegang hebben tot de accountsleutel of de machtiging Microsoft.Storage/storageAccounts/listkeys/action toegewezen hebben gekregen. Zie Een service-SAS maken of Een account-SAS maken voor meer informatie.

Opmerking

Een SAS voor gebruikersdelegering biedt betere beveiliging dan een SAS die is getekend met de sleutel van het opslagaccount. Microsoft raadt het gebruik van een SAS voor gebruikersdelegering aan, indien mogelijk. Zie Beperkte toegang verlenen tot gegevens met Shared Access Signatures (SAS) voor meer informatie.

This article shows how to use the storage account key to create an account SAS with the Azure Storage client library for .NET.

About the account SAS

An account SAS is created at the level of the storage account. By creating an account SAS, you can:

  • Delegate access to service-level operations that aren't currently available with a service-specific SAS, such as Get Blob Service Properties, Set Blob Service Properties and Get Blob Service Stats.
  • Delegate access to more than one service in a storage account at a time. For example, you can delegate access to resources in both Azure Blob Storage and Azure Files by using an account SAS.

Stored access policies aren't supported for an account SAS.

Een account-SAS maken

An account SAS is signed with the account access key. You can use the StorageSharedKeyCredential class to create the credential that is used to sign the SAS.

The following code example shows how to create a new AccountSasBuilder object and call the ToSasQueryParameters method to get the account SAS token string.

public static async Task<string> CreateAccountSAS(StorageSharedKeyCredential sharedKey)
{
    // Create a SAS token that's valid for one day
    AccountSasBuilder sasBuilder = new AccountSasBuilder()
    {
        Services = AccountSasServices.Blobs | AccountSasServices.Queues,
        ResourceTypes = AccountSasResourceTypes.Service,
        ExpiresOn = DateTimeOffset.UtcNow.AddDays(1),
        Protocol = SasProtocol.Https
    };

    sasBuilder.SetPermissions(AccountSasPermissions.Read |
        AccountSasPermissions.Write);

    // Use the key to get the SAS token
    string sasToken = sasBuilder.ToSasQueryParameters(sharedKey).ToString();

    return sasToken;
}

Use an account SAS from a client

To use the account SAS to access service-level APIs for the Blob service, create a BlobServiceClient object using the account SAS and the Blob Storage endpoint for your storage account.

string accountName = "<storage-account-name>";
string accountKey = "<storage-account-key>";
StorageSharedKeyCredential storageSharedKeyCredential =
    new(accountName, accountKey);

// Create a BlobServiceClient object with the account SAS appended
string blobServiceURI = $"https://{accountName}.blob.core.windows.net";
string sasToken = await CreateAccountSAS(storageSharedKeyCredential);
BlobServiceClient blobServiceClientAccountSAS = new BlobServiceClient(
    new Uri($"{blobServiceURI}?{sasToken}"));

Hulpmiddelen

To learn more about creating an account SAS using the Azure Blob Storage client library for .NET, see the following resources.

Bronnen van cliëntbibliotheek

Zie ook