Skip to content

SAS Tokens

๐ŸŽฏ Lab Objective

In this hands-on lab, you will learn how to:

  • Understand Shared Access Signature (SAS) tokens and their security benefits
  • Create different types of SAS tokens (Account, Service, and User Delegation)
  • Configure SAS permissions and expiration times for secure access control
  • Test SAS token functionality from different clients and scenarios
  • Implement time-based access control using SAS token expiration
  • Troubleshoot SAS token issues and understand security implications

Scenario: Your organization needs to provide temporary, secure access to Azure Storage resources for external partners and applications without sharing storage account keys. Youโ€™ll create and manage SAS tokens with different permission levels and expiration times.


๐Ÿ—๏ธ Pre-Provisioned Environment

The following Azure resources have been pre-deployed in your environment:

Resource Overview

Resource TypeResource NameConfigurationPurpose
Resource GroupSASTokens-Lab-RGContains all lab resourcesLogical container
Storage Accountsastokenlab[unique]General Purpose v2Primary storage resource
Blob ContainerdocumentsPrivate access levelDocument storage
Blob ContainerimagesPrivate access levelImage storage
Blob Containerpublic-dataPrivate access levelPublicly accessible data
Test VMClientVMWindows Server 2019SAS token testing client
Sample FilesVariousPre-uploaded contentTest data for SAS operations

Storage Structure

sastokenlab[unique] (Storage Account)
โ”œโ”€โ”€ documents (Container)
โ”‚ โ”œโ”€โ”€ report.pdf
โ”‚ โ”œโ”€โ”€ contract.docx
โ”‚ โ””โ”€โ”€ presentation.pptx
โ”œโ”€โ”€ images (Container)
โ”‚ โ”œโ”€โ”€ logo.png
โ”‚ โ”œโ”€โ”€ banner.jpg
โ”‚ โ””โ”€โ”€ profile.gif
โ””โ”€โ”€ public-data (Container)
โ”œโ”€โ”€ readme.txt
โ”œโ”€โ”€ changelog.md
โ””โ”€โ”€ license.txt

VM Details

VMPrivate IPOperating SystemPurpose
ClientVM10.0.1.4Windows Server 2019Test SAS token access scenarios

๐Ÿš€ Lab Exercises

Part 1: Explore Storage Account and Containers

Step 1: Navigate to Storage Account

  1. Navigate to SASTokens-Lab-RG resource group
  2. Click on the Storage Account (sastokenlab[unique])
  3. In the left menu, click Containers
  4. Explore the containers: documents, images, public-data

Step 2: View Container Contents

  1. Click on documents container
  2. Observe the files: report.pdf, contract.docx, presentation.pptx
  3. Try to access a file directly:
    • Click on report.pdf
    • Click Generate SAS (weโ€™ll use this later)
    • For now, note that direct access requires authentication

Step 3: Test Unauthenticated Access

  1. Copy the blob URL for report.pdf
  2. Open a new incognito/private browser window
  3. Paste the URL and try to access
  4. Expected Result: โŒ Access should be denied (403 Forbidden)

Part 2: Create Account-Level SAS Token

Step 1: Generate Account SAS

  1. In your storage account, go to Security + networking โ†’ Shared access signature
  2. Configure Account SAS settings:
SettingValuePurpose
Allowed servicesโœ… Blob, โœ… File, โœ… Queue, โœ… TableAll services
Allowed resource typesโœ… Service, โœ… Container, โœ… ObjectAll resource types
Allowed permissionsโœ… Read, โœ… Write, โœ… Delete, โœ… ListFull permissions
Start timeCurrent timeImmediate access
Expiry time+24 hoursValid for 24 hours
Allowed IP addressesLeave emptyNo IP restrictions
Allowed protocolsHTTPS onlySecure transport
  1. Click Generate SAS and connection string

Step 2: Copy SAS Token Information

Save the following information:

  • SAS token: Copy the full token string (starts with ?sv=)
  • Blob service SAS URL: Copy the complete URL
  • Connection string: Copy for programmatic access

Step 3: Test Account SAS Token

  1. Manual URL test:
    • Take the report.pdf URL from earlier
    • Append the SAS token: https://sastokenlab[unique].blob.core.windows.net/documents/report.pdf[SAS-TOKEN]
    • Open in incognito browser window
    • Expected Result: โœ… File should download successfully

Part 3: Create Container-Level SAS Token

Step 1: Generate Container SAS

  1. Navigate to Containers โ†’ images
  2. Click the โ€ฆ menu โ†’ Generate SAS
  3. Configure Container SAS settings:
SettingValuePurpose
Permissionsโœ… Read, โœ… ListRead-only access
Start timeCurrent timeImmediate access
Expiry time+2 hoursShort-term access
Allowed IP addressesLeave emptyNo IP restrictions
Allowed protocolsHTTPS onlySecure transport
  1. Click Generate SAS token and URL

Step 2: Test Container SAS

  1. Copy the Blob SAS URL
  2. Test in incognito browser: Should show XML listing of container contents
  3. Test individual file access:
    • Manually construct URL: https://sastokenlab[unique].blob.core.windows.net/images/logo.png[CONTAINER-SAS-TOKEN]
    • Expected Result: โœ… Should download the image file

Part 4: Create Blob-Level SAS Token

Step 1: Generate Blob SAS

  1. Navigate to documents container
  2. Click on contract.docx
  3. Click Generate SAS
  4. Configure Blob SAS settings:
SettingValuePurpose
Permissionsโœ… Read onlyRead-only access
Start timeCurrent timeImmediate access
Expiry time+1 hourVery short-term access
Allowed IP addressesLeave emptyNo IP restrictions
Allowed protocolsHTTPS onlySecure transport
  1. Click Generate SAS token and URL

Step 2: Test Blob SAS Specificity

  1. Test the generated URL: Should download contract.docx
  2. Try to modify URL for different file:
    • Change contract.docx to report.pdf in the URL
    • Keep the same SAS token
    • Expected Result: โŒ Should fail (SAS is blob-specific)

Part 5: Test from Client VM

Step 1: Connect to Client VM

  1. Navigate to ClientVM
  2. Click Connect โ†’ RDP
  3. Use credentials:
    • Username: azureuser
    • Password: LabPassword123!

Step 2: Test SAS Tokens with PowerShell

From ClientVM, open PowerShell and test:

Terminal window
# Test Account SAS token
$accountSASUrl = "https://sastokenlab[unique].blob.core.windows.net/documents/report.pdf?[ACCOUNT-SAS-TOKEN]"
try {
Invoke-WebRequest -Uri $accountSASUrl -OutFile "C:\temp\report.pdf"
Write-Host "โœ… Account SAS: Successfully downloaded report.pdf"
Get-Item "C:\temp\report.pdf"
} catch {
Write-Host "โŒ Account SAS failed: $($_.Exception.Message)"
}
# Test Container SAS token
$containerSASUrl = "https://sastokenlab[unique].blob.core.windows.net/images?[CONTAINER-SAS-TOKEN]&restype=container&comp=list"
try {
$response = Invoke-WebRequest -Uri $containerSASUrl
Write-Host "โœ… Container SAS: Successfully listed container contents"
$response.Content
} catch {
Write-Host "โŒ Container SAS failed: $($_.Exception.Message)"
}
# Test Blob SAS token
$blobSASUrl = "https://sastokenlab[unique].blob.core.windows.net/documents/contract.docx?[BLOB-SAS-TOKEN]"
try {
Invoke-WebRequest -Uri $blobSASUrl -OutFile "C:\temp\contract.docx"
Write-Host "โœ… Blob SAS: Successfully downloaded contract.docx"
} catch {
Write-Host "โŒ Blob SAS failed: $($_.Exception.Message)"
}

Step 3: Test Different Operations

Terminal window
# Try to upload with read-only SAS (should fail)
$readOnlySAS = "[BLOB-SAS-TOKEN-READ-ONLY]"
$uploadUrl = "https://sastokenlab[unique].blob.core.windows.net/documents/newfile.txt?$readOnlySAS"
try {
Invoke-WebRequest -Uri $uploadUrl -Method PUT -Body "Test content" -Headers @{"x-ms-blob-type"="BlockBlob"}
Write-Host "โœ… Upload succeeded (unexpected)"
} catch {
Write-Host "โŒ Upload blocked by read-only SAS (expected behavior)"
}

Part 6: Time-Based Access Control

Step 1: Create Short-Term SAS

  1. Go back to documents container
  2. Click on presentation.pptx
  3. Generate SAS with 1 minute expiry:
SettingValue
PermissionsRead only
Expiry time+1 minute
  1. Copy the SAS URL

Step 2: Test Time Expiration

  1. Immediately test the URL - should work
  2. Wait 2 minutes
  3. Test again - should fail with authentication error
  4. Expected behavior:
    • โœ… Before expiry: File downloads successfully
    • โŒ After expiry: 403 Forbidden error

Part 7: Advanced SAS Configuration

Step 1: Create SAS with IP Restrictions

  1. Get your current public IP address:

  2. Generate new container SAS for public-data:

SettingValuePurpose
PermissionsRead, ListRead access
Expiry time+4 hoursExtended access
Allowed IP addressesYour public IPIP restriction
  1. Test from different locations:
    • โœ… From your IP: Should work
    • โŒ From different IP: Should be blocked

Step 2: Create SAS with Custom Permissions

  1. Generate Account SAS with limited permissions:
SettingValuePurpose
Allowed servicesโœ… Blob onlyBlob service only
Allowed resource typesโœ… Object onlyFiles only, no containers
Allowed permissionsโœ… Read onlyRead-only access
Expiry time+12 hoursHalf-day access
  1. Test limitations:
    • โœ… Can access individual blobs
    • โŒ Cannot list containers
    • โŒ Cannot write/delete

๐Ÿ”ง Troubleshooting Guide

Common SAS Token Issues

IssueSymptomsPossible CauseSolution
403 ForbiddenAccess denied errorSAS expired or invalidGenerate new SAS token
404 Not FoundResource not foundWrong resource path in SASVerify blob/container path
Signature mismatchAuthentication failedURL encoding issuesUse proper URL encoding
IP forbiddenAccess blockedRequest from non-allowed IPCheck IP restrictions
Time-related errorsClock skew issuesClient time mismatchCheck system time

SAS Token Validation

CheckMethodExpected Result
Token formatVerify starts with ?sv=Valid SAS parameter string
Expiry timeCheck se parameterFuture timestamp
PermissionsCheck sp parameterRequired permission letters
ResourceCheck sr parameterCorrect resource type

๐Ÿงช Additional Experiments

Try these optional exercises to deepen your understanding:

  • User Delegation SAS: Create SAS using Azure AD credentials instead of account keys
  • Stored Access Policies: Create reusable access policies for consistent SAS generation
  • SAS with Azure CLI: Generate SAS tokens using command-line tools
  • Application Integration: Use SAS tokens in web applications for secure file uploads
  • Monitoring: Track SAS token usage in storage analytics logs

๐ŸŽ“ Key Takeaways

After completing this lab, you should understand:

  • SAS tokens provide secure, temporary access to storage resources without sharing keys
  • Different SAS types (Account, Service, User Delegation) offer varying scopes of access
  • Time-based access control enables automatic expiration of access permissions
  • Granular permissions allow precise control over allowed operations
  • IP restrictions add an additional layer of network-based security
  • SAS tokens are URL-safe and can be embedded in web applications
  • Proper SAS management is crucial for maintaining storage security

๐Ÿ“Š SAS Token Types Comparison

SAS Token Types

TypeScopeUse CaseSecurity Level
Account SASEntire storage accountAdmin operations, bulk accessHigh permissions
Service SASSpecific service (Blob, File, etc.)Service-specific accessMedium permissions
User Delegation SASAzure AD securedEnterprise scenariosHighest security

Permission Letters

  • r = Read
  • w = Write
  • d = Delete
  • l = List
  • a = Add
  • c = Create
  • u = Update
  • p = Process

๐Ÿ“š Additional Resources