4
Answers

Is it possible to create azure maps SAS token programmatically?

Is it possible to create azure maps SAS token programmatically?

Answers (4)

1
Photo of Saravanan Ganesan
36 35.3k 273.1k 1y

Yes, it is possible to create an Azure Maps Shared Access Signature (SAS) token programmatically. Azure Maps allows you to generate SAS tokens to provide controlled access to your map resources, including data, images, and other assets. SAS tokens are a secure way to grant time-limited access to specific resources without sharing your account keys.

To generate an Azure Maps SAS token programmatically, you can use the Azure Maps REST APIs or one of the Azure Maps SDKs, such as the JavaScript, .NET, or Python SDKs. Below are general steps to create an Azure Maps SAS token programmatically using the .NET SDK:

  1. Install Azure Maps SDK:

    • Install the Azure Maps SDK for .NET in your project using NuGet.
  2. Authenticate with Azure Maps:

    • Use your Azure Maps subscription key or Azure Active Directory (AAD) credentials to authenticate your application with Azure Maps services.
  3. Create a Shared Access Signature:

    • Use the SharedKeyCredential class from the Azure.Identity namespace to create a SAS token for the specific resource you want to access.
  4. Set Token Expiry and Permissions:

    • Define the SAS token's expiry time and specify the permissions you want to grant, such as read, write, or list.
  5. Use the SAS Token:

    • Use the generated SAS token as a query parameter or in the authorization header when making requests to Azure Maps services.

Kindly find the code snippet below : 

using Azure.Identity;
using Azure.Maps.MapControl;
using System;

class Program
{
    static void Main()
    {
        // Replace with your Azure Maps subscription key or AAD credentials
        string subscriptionKey = "YOUR_AZURE_MAPS_SUBSCRIPTION_KEY_OR_AAD_CREDENTIALS";

        // Create a SharedKeyCredential using the subscription key or AAD credentials
        var credential = new SharedKeyCredential(subscriptionKey);

        // Define the SAS token's expiry time (e.g., 1 hour from now)
        DateTimeOffset expiryTime = DateTimeOffset.UtcNow.AddHours(1);

        // Specify the permissions for the SAS token
        var permissions = new List<Permissions> { Permissions.Read, Permissions.Write };

        // Generate the SAS token
        string sasToken = credential.GetSASToken(expiryTime, permissions);

        Console.WriteLine($"Generated SAS token: {sasToken}");
    }
}

 

Remember to replace YOUR_AZURE_MAPS_SUBSCRIPTION_KEY_OR_AAD_CREDENTIALS with your actual Azure Maps subscription key or AAD credentials. The generated SAS token can then be used to access the specified Azure Maps resources within the defined permissions and time window.

1
Photo of Rajkiran Swain
28 40.7k 3.4m 1y

Yes, it is possible to create Azure Maps SAS (Shared Access Signature) tokens programmatically. Azure Maps provides a REST API that allows you to generate SAS tokens dynamically.

Here is an example of how you can create an Azure Maps SAS token programmatically using C#:

  1. Install the Azure Maps package: Install the AzureMapsControl.Components package from NuGet in your project. This package provides the necessary classes and methods for working with Azure Maps.

  2. Generate the SAS token: Use the AzureMapsControl.Components.AzureMapsServices.CreateSasToken method to generate the SAS token. This method takes the necessary parameters such as the resource URL, the primary key or secondary key for your Azure Maps account, and the desired expiration time for the token.

     

    using AzureMapsControl.Components.AzureMapsServices;
    
    // Specify the resource URL for which you want to generate the SAS token
    string resourceUrl = "https://atlas.microsoft.com";
    
    // Specify the primary key or secondary key for your Azure Maps account
    string accountKey = "your-account-key";
    
    // Specify the desired expiration time for the token
    DateTime expirationTime = DateTime.UtcNow.AddMinutes(30);
    
    // Generate the SAS token
    string sasToken = AzureMapsServices.CreateSasToken(resourceUrl, accountKey, expirationTime);
    

     

  3. The sasToken variable will contain the generated SAS token that can be used to authenticate requests to Azure Maps services.

  4. Use the SAS token: Once you have the SAS token, you can include it in your requests to Azure Maps services by appending it as a query parameter. For example:
     

    https://atlas.microsoft.com/search/address/json?api-version=1.0&query=Seattle&subscription-key=your-subscription-key&sas_token=your-sas-token
    

     

  5. Replace your-sas-token with the generated SAS token.

  6. By generating SAS tokens programmatically, you have control over the expiration time and can dynamically generate tokens as needed for your application. Remember to keep the primary or secondary key for your Azure Maps account secure and avoid exposing it in your code or client-side applications.

     

0
Photo of Saravanan Ponnusamy
354 4.5k 1.2m 1y

@rajkiran 

it doesnt worked for me.

am i missing anything?

0
Photo of Saravanan Ponnusamy
354 4.5k 1.2m 1y

Thank You very much for your answer @rajkiran swain. i'll try with development. 

Also, could you pls. share the weblink for the below answer for the reference.