@@ -33,7 +33,12 @@ public class GetAzureMapsSaSToken()
3333 /// This tokenProvider will cache the token in memory, if you would like to reduce the dependency on Azure AD we recommend
3434 /// implementing a distributed cache combined with using the other methods available on tokenProvider.
3535 /// </remarks>
36- private readonly DefaultAzureCredential _TokenProvider = new ( ) ;
36+ private readonly DefaultAzureCredential _TokenProvider = new ( new DefaultAzureCredentialOptions
37+ {
38+ TenantId = Environment . GetEnvironmentVariable ( "MAPS_SAS_TENANT_ID" ) ?? "" ,
39+ ExcludeInteractiveBrowserCredential = true ,
40+ ExcludeEnvironmentCredential = true
41+ } ) ;
3742
3843 private ArmClient armClient ;
3944
@@ -64,17 +69,17 @@ public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] Http
6469 /// <returns>The SAS token string</returns>
6570 private string GenerateAzureMapsSasToken ( ArmClient armClient , int expiryInSeconds = 600 , int maxRatePerSecond = 50 )
6671 {
67- string subscriptionId = Environment . GetEnvironmentVariable ( "AZURE_MAPS_SUBSCRIPTION_ID" ) ;
68- string resourceGroupName = Environment . GetEnvironmentVariable ( "AZURE_MAPS_RESOURCE_GROUP_NAME" ) ;
69- string accountName = Environment . GetEnvironmentVariable ( "AZURE_MAPS_ACCOUNT_NAME" ) ;
72+ string subscriptionId = Environment . GetEnvironmentVariable ( "AZURE_MAPS_SUBSCRIPTION_ID" ) ?? "" ;
73+ string resourceGroupName = Environment . GetEnvironmentVariable ( "AZURE_MAPS_RESOURCE_GROUP_NAME" ) ?? "" ;
74+ string accountName = Environment . GetEnvironmentVariable ( "AZURE_MAPS_ACCOUNT_NAME" ) ?? "" ;
7075
7176 // Get maps account resource
7277 ResourceIdentifier mapsAccountResourceId = MapsAccountResource . CreateResourceIdentifier ( subscriptionId , resourceGroupName , accountName ) ;
7378 MapsAccountResource mapsAccount = armClient . GetMapsAccountResource ( mapsAccountResourceId ) ;
7479
7580 // Assign SAS token information
7681 // Every time you want to SAS token, update the principal ID, max rate, start and expiry time
77- string principalId = Environment . GetEnvironmentVariable ( "AZURE_MAPS_PRINCIPAL_ID" ) ;
82+ string principalId = Environment . GetEnvironmentVariable ( "AZURE_MAPS_PRINCIPAL_ID" ) ?? "" ;
7883
7984 // Set start and expiry time for the SAS token in round-trip date/time format
8085 DateTime start = DateTime . Now ;
@@ -84,7 +89,7 @@ private string GenerateAzureMapsSasToken(ArmClient armClient, int expiryInSecond
8489 // The MapsSigningKey is used to specify the type of signing key to use for the SAS token
8590 // In this case, we are using primary key as the signing key
8691 MapsAccountSasContent sasContent = new MapsAccountSasContent (
87- MapsSigningKey . PrimaryKey ,
92+ MapsSigningKey . ManagedIdentity ,
8893 principalId ,
8994 maxRatePerSecond ,
9095 start . ToString ( "O" ) ,
@@ -93,10 +98,7 @@ private string GenerateAzureMapsSasToken(ArmClient armClient, int expiryInSecond
9398 // Get SAS token
9499 Response < MapsAccountSasToken > sas = mapsAccount . GetSas ( sasContent ) ;
95100
96- // Create a SearchClient that will authenticate via SAS token
97- AzureSasCredential sasCredential = new ( sas . Value . AccountSasToken ) ;
98-
99101 // Return SAS token signature in string format
100- return sasCredential . Signature ;
102+ return sas . Value . AccountSasToken ;
101103 }
102104}
0 commit comments