Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 11 of 47
❮Prev Next❯

SAS Tokens and Stored Access Policies

Module 2: Storage ⏱ 12 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    30-second answer

    A shared access signature (SAS) is a signed URL token that grants limited permissions (such as read and list) on specific storage resources until an expiry time. There are three kinds: a user delegation SAS (signed with Microsoft Entra credentials, Blob Storage, at most 7 days, the recommended one), a service SAS (signed with an account key, one service) and an account SAS (signed with an account key, one or more services). A stored access policy on a container, share, queue or table (up to 5 each) holds the permissions and expiry for service SAS tokens that reference it, so deleting or changing the policy revokes them without rotating the keys.

    Simulate it » No Azure account needed

    Exam objective: 2.1.2 Create and use shared access signature (SAS) tokens. 2.1.3 Configure stored access policies. (Skills measured as of April 17, 2026)

    Simulations

    Feel it before you read it. No account needed, nothing leaves this page.

    Azure portal: Stored access policy and a service SAS for a container Add a stored access policy and generate a SAS that points at it.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading the portal…

    Open <strong>Containers</strong> and select <strong>invoices</strong>. Auditors need read access for a month.

    👆 Click the glowing item in the portal to continue

    Read the steps as text
    1. Open Containers and select invoices. Auditors need read access for a month.
    2. Under Settings pick Access policy, then + Add policy in Stored access policies.
    3. Name it auditors-read, give Read and List, and an expiry 30 days out.
    4. Select Save. Nothing changes until you save the container’s access policy.
    5. Now Shared access tokens. Pick Account key, choose the auditors-read policy, and it fills in permissions and expiry. HTTPS only.
    6. Copy the token. To end access early, delete the policy (or change its expiry). An ad hoc SAS without a policy can only be killed by rotating the key that signed it.
    7. For the strongest option pick User delegation key instead: the SAS is signed with your Microsoft Entra credentials, lasts at most 7 days and can’t use a stored access policy.

    🧪 Simulation for learning. Screens are simplified versions of the Azure portal; names, prices and IDs are examples. Nothing here touches real Azure.

    😟 THE PROBLEM

    An auditor needs a month of read access, and you may need to cut it off early.

    😊 THE SOLUTION

    Give a service SAS tied to a stored access policy you can delete.

    • Sharing the account key✓ Scoped, expiring SAS
    • Access must end early✓ Delete the policy
    • Want no key at all✓ User delegation SAS
    🛠️ Solved with: SAS + stored access policy

    Start with a question

    How do you let a plumber into your flat on Tuesday without giving them your house key forever?

    You don’t hand over your own key. You give the building guard a note: “Let the plumber into flat 4B, kitchen only, Tuesday 10 to 12.” The plumber shows the note, the guard checks it, and on Wednesday the note is worthless.

    Better still, the guard keeps a clipboard with rules like “plumbers: kitchen only, until Friday”. The note just says “see clipboard, plumbers”. If you change your mind, you cross out the line on the clipboard and every plumber note stops working at once.

    The note is a SAS. The clipboard line is a stored access policy.

    Explain it like I’m 10

    The technical explanation

    A SAS is a query string you append to a storage URL. It carries the permissions (sp), expiry (se), optional start (st), allowed IPs (sip), protocol (spr=https), the resource type and a signature (sig). Storage recomputes the signature with the same key; if anything was changed, the request fails.

    Three kinds of SAS

    User delegation SASService SASAccount SAS
    Signed withUser delegation key from Microsoft Entra IDAccount keyAccount key
    CoversBlob Storage (containers and blobs)One service (blob, file, queue or table)One or more services, service-level operations too
    Max lifetime7 daysWhatever you setWhatever you set
    Stored access policyNoYesNo
    Works with key access disabledYesNoNo
    Revoke earlyRevoke delegation keys or the signer’s roleDelete the policy, or regenerate the keyRegenerate the key

    Microsoft recommends the user delegation SAS wherever it fits. The signer needs permission to get a user delegation key, and at use time the SAS can’t do more than the signer’s own data role allows.

    Stored access policies

    A stored access policy lives on a container, file share, queue or table, and each can hold up to five. It holds any of: permissions, start time, expiry time. A service SAS names it with si=<policy> and takes the missing fields from the policy. Don’t set the same field in both places; storage rejects that.

    To revoke every SAS tied to a policy, delete the policy, rename it, or set its expiry in the past. Changes can take up to 30 seconds to apply. An ad hoc SAS (no policy) can only be revoked by regenerating the key that signed it.

    Portal steps

    1. Open the container, then Settings > Access policy > + Add policy. Enter the identifier, tick the permissions, set start and expiry, then Save.
    2. Open Settings > Shared access tokens. Choose Signing method (Account key or User delegation key), the signing key, the Stored access policy, allowed IPs and HTTPS only.
    3. Select Generate SAS token and URL and copy it straight away. Azure doesn’t keep a copy.

    The account-wide Shared access signature blade (under Security + networking) creates account SAS tokens.

    CLI and PowerShell

    EXP=$(date -u -d "+30 days" '+%Y-%m-%dT%H:%MZ')
    
    # Stored access policy + service SAS that uses it
    az storage container policy create --account-name stcontosoprod -c invoices \
      -n auditors-read --permissions rl --expiry $EXP
    az storage container generate-sas --account-name stcontosoprod -n invoices \
      --policy-name auditors-read --https-only -o tsv
    
    # Revoke every SAS tied to the policy
    az storage container policy delete --account-name stcontosoprod -c invoices -n auditors-read
    
    # User delegation SAS for one blob (max 7 days, no account key)
    az storage blob generate-sas --account-name stcontosoprod -c invoices -n invoice-1001.pdf \
      --permissions r --expiry 2026-11-30T18:00Z --auth-mode login --as-user --https-only --full-uri
    $ctx = (Get-AzStorageAccount -ResourceGroupName rg-storage-prod -Name stcontosoprod).Context
    New-AzStorageContainerStoredAccessPolicy -Container invoices -Policy auditors-read `
      -Permission rl -ExpiryTime (Get-Date).AddDays(30) -Context $ctx
    New-AzStorageContainerSASToken -Name invoices -Policy auditors-read -Protocol HttpsOnly -Context $ctx
    Architect level SAS expiration policies and good hygiene

    You can set a SAS expiration policy on the account (for example, one day). Azure then logs any SAS signed with an account key whose validity is longer, so you can find bad habits. Other habits that pay off: keep expiry short, use HTTPS only, restrict sip to known ranges, give only the letters you need (r and l, not racwdl), and prefer user delegation SAS so a leaked account key isn’t part of the story at all.

    Real-world business scenario

    Professional services

    Fabrikam Accounting (fictional)

    The problem
    External auditors were emailed the storage account key every year, and nobody remembered to rotate it afterwards.
    What they used
    A stored access policy auditors-read on the invoices container (read and list, 30 days), and a service SAS with HTTPS only and the auditors' office IP range.
    Why that choice
    Auditors only see one container, only read, only from their office, and the team can end access in seconds by deleting the policy.
    The result
    When the audit finished a week early, the admin deleted the policy and confirmed the SAS returned 403. The account keys never left the team.

    When to use it and when not to

    ✓ Use it when…

    • Someone outside your tenant needs time-limited access to specific containers or blobs.
    • A client app uploads directly to storage and you hand it a short-lived token.
    • You need to be able to revoke a group of tokens in one move (use a stored access policy).

    ✕ Think twice when…

    • The caller is your own app or user with a Microsoft Entra identity (use RBAC data roles).
    • You need long-lived access with no expiry (rethink the design).
    • You want to revoke an ad hoc SAS without touching the keys (you can't).

    ↔ Alternatives to consider

    • Microsoft Entra ID with Storage Blob Data roles for your own apps and staff.
    • User delegation SAS instead of key-signed SAS where Blob Storage is used.
    • Storage firewall to limit where a SAS can be used from.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    Test yourself with exercises

    5 original practice questions in the AZ-104 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.

    Question 1

    Question 1 of 5

    You issue service SAS tokens for the container invoices. You must be able to revoke all of the tokens at once without regenerating the storage account keys. What should you do?

    Question 2

    Question 2 of 5

    A storage account has Allow storage account key access set to Disabled. An application must give a partner temporary read access to one blob. Which type of SAS should you use?

    Question 3

    Question 3 of 5

    What is the maximum validity period of a user delegation SAS?

    Question 4

    Question 4 of 5

    A container already has five stored access policies. You attempt to add a sixth policy. What happens?

    Question 5

    Question 5 of 5 · Select 2

    A web application must allow clients to upload files directly to the uploads container for 15 minutes, over HTTPS only. The solution must not grant access to any other container. Which two settings should the SAS include? Each correct answer presents part of the solution.

    Interview preparation

    Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.

    Beginner Why would you give someone a SAS instead of the storage account key?

    Model answer

    The key gives full control of everything in the account, forever. A SAS gives only the permissions I choose, on the container or blob I choose, until a time I choose, and I can require HTTPS or a specific IP range. If it leaks, the damage is limited.

    Intermediate You gave a partner an ad hoc service SAS valid for six months and now need to cut them off. What can you do?

    Model answer

    An ad hoc SAS carries its own permissions and expiry, so the only way to revoke it early is to regenerate the account key that signed it. That also breaks anything else using that key, so I'd move other apps to the second key first. Next time I'd tie the SAS to a stored access policy so I can just delete the policy.

    Experienced developer When would you choose a user delegation SAS?

    Model answer

    Whenever the data is in Blob Storage and the person creating the SAS has a Microsoft Entra identity. It's signed with a key Entra ID issues, not the account key, so it still works when Shared Key access is disabled, it's capped at seven days and its rights can never exceed what the signer's RBAC role allows. I can revoke all of them with az storage account revoke-delegation-keys.

    Scenario Auditors need read access to the invoices container for 30 days, and you might have to end it early. How do you set it up?

    Model answer

    Create a stored access policy called auditors-read on the invoices container with read and list and a 30-day expiry. Then generate a service SAS with --policy-name auditors-read, HTTPS only. To end access I delete the policy, or change its expiry to the past. The account keys don't change.

    Quick revision

    Key points

    • User delegation SAS = Microsoft Entra ID, Blob Storage, max 7 days, recommended.
    • Service SAS = account key, one service. Account SAS = account key, several services.
    • Stored access policies (max 5 per container) work only with service SAS tokens.
    • Revoke a policy-based SAS by deleting or editing the policy. Revoke an ad hoc SAS by regenerating its signing key.
    • Policy changes can take up to 30 seconds to apply.
    • Use HTTPS only, short expiry and the least permissions.

    Important terminology

    Shared access signature (SAS)
    A token appended to a storage URL that grants specific permissions on specific resources for a limited time.
    User delegation SAS
    A SAS signed with a user delegation key that Microsoft Entra ID issues, so no account key is involved. Valid for up to 7 days.
    Service SAS
    A SAS signed with an account key that grants access to resources in one service, such as one blob container.
    Account SAS
    A SAS signed with an account key that can cover one or more services and service-level operations.
    Stored access policy
    A named set of permissions, start and expiry time saved on a container, share, queue or table, which service SAS tokens can reference.
    Signed identifier (si)
    The SAS parameter that names the stored access policy the token uses.

    Common mistakes

    • Trying to attach a stored access policy to an account SAS or a user delegation SAS.
    • Setting the same field (permissions or expiry) in both the policy and the SAS.
    • Forgetting that disabling Shared Key access also kills service and account SAS tokens.

    Exam tips

    • "Revoke access without regenerating keys" = stored access policy.
    • "SAS secured with Microsoft Entra credentials" = user delegation SAS.
    • "One SAS for blobs and queues" = account SAS.

    Five quick revision questions

    1. 1.How many stored access policies can one container have?Show

      Five.

    2. 2.What is the maximum validity of a user delegation SAS?Show

      Seven days.

    3. 3.Which SAS types can reference a stored access policy?Show

      Service SAS only.

    4. 4.How do you revoke an ad hoc service SAS early?Show

      Regenerate the account key that signed it.

    5. 5.Which SAS parameter names the stored access policy?Show

      si (signed identifier).

    Official references

    Finished this lesson? Tick it off. Your progress is saved in this browser.

    Next up: Manage and Rotate Storage Access Keys

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.