Skip to content

Commit e6393d3

Browse files
committed
Add GetAzureMapsSasToken API
1 parent 6d4cd87 commit e6393d3

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using Azure;
2+
using Azure.Core;
3+
using Azure.Identity;
4+
using Azure.ResourceManager;
5+
using Azure.ResourceManager.Maps;
6+
using Azure.ResourceManager.Maps.Models;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Azure.Functions.Worker;
10+
using System;
11+
using System.Globalization;
12+
using System.Net.Http;
13+
using System.Security.Cryptography;
14+
using System.Text;
15+
using System.Threading.Tasks;
16+
using System.Web;
17+
18+
namespace SampleFunctions;
19+
20+
public class GetAzureMapsSaSToken()
21+
{
22+
private static readonly string[] AllowedDomains = [
23+
"https://samples.azuremaps.com/",
24+
"http://localhost:58035/" // For local testing
25+
];
26+
27+
/// <summary>
28+
/// This token provider simplifies access tokens for Azure Resources. It uses the Managed Identity of the deployed resource.
29+
/// For instance if this application was deployed to Azure App Service or Azure Virtual Machine, you can assign an Azure AD
30+
/// identity and this library will use that identity when deployed to production.
31+
/// </summary>
32+
/// <remarks>
33+
/// This tokenProvider will cache the token in memory, if you would like to reduce the dependency on Azure AD we recommend
34+
/// implementing a distributed cache combined with using the other methods available on tokenProvider.
35+
/// </remarks>
36+
private readonly DefaultAzureCredential _TokenProvider = new();
37+
38+
// The scopes for the access token
39+
private readonly string[] _Scopes =
40+
[
41+
"https://atlas.microsoft.com/.default"
42+
];
43+
44+
private ArmClient armClient;
45+
46+
[Function("GetAzureMapsSaSToken")]
47+
public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req)
48+
{
49+
// Check if the referer header is present and if the domain is allowed
50+
if (req.Headers.TryGetValue("Referer", out var referer) && AllowedDomains.Any(domain => referer.ToString().StartsWith(domain)))
51+
{
52+
armClient = new ArmClient(_TokenProvider);
53+
54+
// Generate SAS token for Azure Maps
55+
string sasToken = GenerateAzureMapsSasToken(armClient);
56+
57+
return new OkObjectResult(sasToken);
58+
}
59+
60+
// Return access denied if the referer domain is not allowed
61+
return new StatusCodeResult(StatusCodes.Status403Forbidden);
62+
}
63+
64+
/// <summary>
65+
/// Generates a Shared Access Signature (SAS) token for Azure Maps authentication.
66+
/// </summary>
67+
/// <param name="armClient">The ArmClient instance to use for authentication</param>
68+
/// <param name="expiryInMinutes">Token expiry time in seconds (default: 600)</param>
69+
/// <param name="maxRatePerSecond">Maximum rate per second (default: 100)</param>
70+
/// <returns>The SAS token string</returns>
71+
private string GenerateAzureMapsSasToken(ArmClient armClient, int expiryInSeconds = 600, int maxRatePerSecond = 50)
72+
{
73+
string subscriptionId = Environment.GetEnvironmentVariable("AZURE_MAPS_SUBSCRIPTION_ID");
74+
string resourceGroupName = Environment.GetEnvironmentVariable("AZURE_MAPS_RESOURCE_GROUP_NAME");
75+
string accountName = Environment.GetEnvironmentVariable("AZURE_MAPS_ACCOUNT_NAME");
76+
77+
// Get maps account resource
78+
ResourceIdentifier mapsAccountResourceId = MapsAccountResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, accountName);
79+
MapsAccountResource mapsAccount = armClient.GetMapsAccountResource(mapsAccountResourceId);
80+
81+
// Assign SAS token information
82+
// Every time you want to SAS token, update the principal ID, max rate, start and expiry time
83+
string principalId = Environment.GetEnvironmentVariable("AZURE_MAPS_PRINCIPAL_ID");
84+
85+
// Set start and expiry time for the SAS token in round-trip date/time format
86+
DateTime start = DateTime.Now;
87+
DateTime expiry = start.AddSeconds(expiryInSeconds);
88+
89+
// Create the SAS token content
90+
// The MapsSigningKey is used to specify the type of signing key to use for the SAS token
91+
// In this case, we are using a managed identity
92+
MapsAccountSasContent sasContent = new MapsAccountSasContent(
93+
MapsSigningKey.ManagedIdentity,
94+
principalId,
95+
maxRatePerSecond,
96+
start.ToString("O"),
97+
expiry.ToString("O"));
98+
99+
// Get SAS token
100+
Response<MapsAccountSasToken> sas = mapsAccount.GetSas(sasContent);
101+
102+
// Create a SearchClient that will authenticate via SAS token
103+
AzureSasCredential sasCredential = new(sas.Value.AccountSasToken);
104+
105+
// Return SAS token signature in string format
106+
return sasCredential.Signature;
107+
}
108+
}

‎Services/SampleFunctions/SampleFunctions.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<ItemGroup>
1010
<FrameworkReference Include="Microsoft.AspNetCore.App" />
1111
<PackageReference Include="Azure.Identity" Version="1.13.2" />
12+
<PackageReference Include="Azure.ResourceManager" Version="1.13.1" />
13+
<PackageReference Include="Azure.ResourceManager.Maps" Version="1.1.0" />
1214
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
1315
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.3.0" />
1416
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.1" />

0 commit comments

Comments
 (0)