Skip to content

Commit 25ff0dc

Browse files
authored
Merge pull request #217878 from stevemunk/SM-Rest-SDK-Developers-Guide-CSharp-Add-Auth
Added auth to C# SDK guide
2 parents 0c8ba7f + d6cf00b commit 25ff0dc

File tree

3 files changed

+96
-10
lines changed

3 files changed

+96
-10
lines changed

articles/azure-maps/how-to-dev-guide-csharp-sdk.md

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ titleSuffix: Azure Maps
44
description: How to develop applications that incorporate Azure Maps using the C# SDK Developers Guide.
55
author: stevemunk
66
ms.author: v-munksteve
7-
ms.date: 10/31/2021
7+
ms.date: 11/11/2021
88
ms.topic: how-to
99
ms.service: azure-maps
1010
services: azure-maps
1111
---
1212

1313
# C# REST SDK Developers Guide
1414

15-
The Azure Maps C# SDK supports all of the functionality provided in the [Azure Maps Rest API][Rest API], like searching for an address, routing between different coordinates, and getting the geo-location of a specific IP address. This article will help you get started building location-aware applications that incorporate the power of Azure Maps.
15+
The Azure Maps C# SDK supports functionality available in the [Azure Maps Rest API][Rest API], like searching for an address, routing between different coordinates, and getting the geo-location of a specific IP address. This article introduces the C# REST SDK with examples to help you get started building location-aware applications in C# that incorporate the power of Azure Maps.
1616

1717
> [!NOTE]
1818
> Azure Maps C# SDK supports any .NET version that is compatible with [.NET standard 2.0][.NET standard]. For an interactive table, see [.NET Standard versions][.NET Standard versions].
@@ -59,18 +59,93 @@ dotnet add package Azure.Maps.Geolocation --prerelease
5959
| [Rendering][rendering readme]| [Azure.Maps.Rendering][rendering package]|[rendering sample][rendering sample] |
6060
| [Geolocation][geolocation readme]|[Azure.Maps.Geolocation][geolocation package]|[geolocation sample][geolocation sample]|
6161

62+
## Create and authenticate a MapsSearchClient
63+
64+
The client object used to access the Azure Maps Search APIs require either an `AzureKeyCredential` object to authenticate when using an Azure Maps subscription key or a `TokenCredential` object with the Azure Maps client ID when authenticating using Azure Active Directory (Azure AD). For more information on authentication, see [Authentication with Azure Maps][authentication].
65+
66+
### Using an Azure AD credential
67+
68+
You can authenticate with Azure AD using the [Azure Identity library][Identity library .NET]. To use the [DefaultAzureCredential][defaultazurecredential.NET] provider, you'll need to install the Azure Identity client library for .NET:
69+
70+
```powershell
71+
dotnet add package Azure.Identity
72+
```
73+
74+
You'll need to register the new Azure AD application and grant access to Azure Maps by assigning the required role to your service principal. For more information, see [Host a daemon on non-Azure resources][Host daemon]. During this process you'll get an Application (client) ID, a Directory (tenant) ID, and a client secret. Copy these values and store them in a secure place. You'll need them in the following steps.
75+
76+
Set the values of the Application (client) ID, Directory (tenant) ID, and client secret of your Azure AD application, and the map resource’s client ID as environment variables:
77+
78+
| Environment Variable | Description |
79+
|----------------------|---------------------------------------------------------------|
80+
| AZURE_CLIENT_ID | Application (client) ID in your registered application |
81+
| AZURE_CLIENT_SECRET | The value of the client secret in your registered application |
82+
| AZURE_TENANT_ID | Directory (tenant) ID in your registered application |
83+
| MAPS_CLIENT_ID | The client ID in your Azure Map resource |
84+
85+
Now you can create environment variables in PowerShell to store these values:
86+
87+
```powershell
88+
$Env:AZURE_CLIENT_ID="Application (client) ID"
89+
$Env:AZURE_CLIENT_SECRET="your client secret"
90+
$Env:AZURE_TENANT_ID="your Directory (tenant) ID"
91+
$Env:MAPS_CLIENT_ID="your Azure Maps client ID"
92+
```
93+
94+
After setting up the environment variables, you can use them in your program to instantiate the `AzureMapsSearch` client:
95+
96+
```csharp
97+
using System;
98+
using Azure.Identity;
99+
using Azure.Maps.Search;
100+
101+
var credential = new DefaultAzureCredential();
102+
var clientId = Environment.GetEnvironmentVariable("MAPS_CLIENT_ID");
103+
var client = new MapsSearchClient(credential, clientId);
104+
105+
```
106+
107+
> [!IMPORTANT]
108+
> The other environment variables created above, while not used in the code sample here, are required by `DefaultAzureCredential()`. If you do not set these environment variables correctly, using the same naming conventions, you will get run-time errors. For example, if your `AZURE_CLIENT_ID` is missing or invalid you will get an `InvalidAuthenticationTokenTenant` error.
109+
110+
### Using a subscription key credential
111+
112+
You can authenticate with your Azure Maps subscription key. Your subscription key can be found in the **Authentication** section in the Azure Maps account as shown in the following screenshot:
113+
114+
:::image type="content" source="./media/rest-sdk-dev-guides/subscription-key.png" alt-text="A screenshot showing the subscription key in the Authentication section of an Azure Maps account." lightbox="./media/rest-sdk-dev-guides/subscription-key.png":::
115+
116+
Now you can create environment variables in PowerShell to store the subscription key:
117+
118+
```powershell
119+
$Env:SUBSCRIPTION_KEY="your subscription key"
120+
```
121+
122+
Once your environment variable is created, you can access it in your code:
123+
124+
```csharp
125+
using System;
126+
using Azure;
127+
using Azure.Maps.Search;
128+
129+
// Use Azure Maps subscription key authentication
130+
var subscriptionKey = Environment.GetEnvironmentVariable("SUBSCRIPTION_KEY") ?? string.Empty;
131+
var credential = new AzureKeyCredential(subscriptionKey);
132+
var client = new MapsSearchClient(credential);
133+
```
134+
62135
### Fuzzy search an entity
63136

64137
The following code snippet demonstrates how, in a simple console application, to import the `Azure.Maps.Search` package and perform a fuzzy search on“Starbucks” near Seattle. In `Program.cs`:
65138

66139
```csharp
140+
using System;
67141
using Azure;
68142
using Azure.Core.GeoJson;
69143
using Azure.Maps.Search;
70144
using Azure.Maps.Search.Models;
71145

72146
// Use Azure Maps subscription key authentication
73-
var credential = new AzureKeyCredential("Azure_Maps_Subscription_key");
147+
var subscriptionKey = Environment.GetEnvironmentVariable("SUBSCRIPTION_KEY") ?? string.Empty;
148+
var credential = new AzureKeyCredential(subscriptionKey);
74149
var client = new MapsSearchClient(credential);
75150

76151
SearchAddressResult searchResult = client.FuzzySearch(
@@ -93,7 +168,7 @@ foreach (var result in searchResult.Results)
93168
}
94169
```
95170

96-
In the above code snippet, you create a `MapsSearchClient` object using your Azure credentials, then use that Search Client's [FuzzySearch][FuzzySearch] method passing in the point of interest (POI) name "_Starbucks_" and coordinates _GeoPosition(-122.31, 47.61)_. This all gets wrapped up by the SDK and sent to the Azure Maps REST endpoints. When the search results are returned, they're written out to the screen using `Console.WriteLine`.
171+
The above code snippet demonstrates how to create a `MapsSearchClient` object using your Azure credentials, then uses its [FuzzySearch][FuzzySearch] method, passing in the point of interest (POI) name "_Starbucks_" and coordinates _GeoPosition(-122.31, 47.61)_. This all gets wrapped up by the SDK and sent to the Azure Maps REST endpoints. When the search results are returned, they're written out to the screen using `Console.WriteLine`.
97172

98173
The following libraries are used:
99174

@@ -158,8 +233,9 @@ Call the `SearchAddress` method to get the coordinate of an address. Modify the
158233

159234
```csharp
160235
// Use Azure Maps subscription key authentication
161-
var credential = new AzureKeyCredential("Azure_Maps_Subscription_key");
162-
var client = new MapsSearchClient(credential);
236+
var subscriptionKey = Environment.GetEnvironmentVariable("SUBSCRIPTION_KEY") ?? string.Empty;
237+
var credential = new AzureKeyCredential(subscriptionKey);
238+
var client = new MapsSearchClient(credential);
163239

164240
SearchAddressResult searchResult = client.SearchAddress(
165241
"1301 Alaskan Way, Seattle, WA 98101, US");
@@ -245,14 +321,16 @@ printReverseBatchAddresses(newOperationResult);
245321
The complete code for reverse address batch search with operation ID:
246322

247323
```csharp
324+
using system;
248325
using Azure;
249326
using Azure.Core.GeoJson;
250327
using Azure.Maps.Search;
251328
using Azure.Maps.Search.Models;
252329

253330
// Use Azure Maps subscription key authentication
254-
var credential = new AzureKeyCredential("Azure_Maps_Subscription_key");
255-
var client = new MapsSearchClient(credential);
331+
var subscriptionKey = Environment.GetEnvironmentVariable("SUBSCRIPTION_KEY") ?? string.Empty;
332+
var credential = new AzureKeyCredential(subscriptionKey);
333+
var client = new MapsSearchClient(credential);
256334

257335
var queries = new List<ReverseSearchAddressQuery>()
258336
{
@@ -317,3 +395,7 @@ The [Azure.Maps Namespace][Azure.Maps Namespace] in the .NET documentation.
317395
[geolocation sample]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/maps/Azure.Maps.Geolocation/samples
318396
[FuzzySearch]: /dotnet/api/azure.maps.search.mapssearchclient.fuzzysearch
319397
[Azure.Maps Namespace]: /dotnet/api/azure.maps
398+
[search-api]: /dotnet/api/azure.maps.search
399+
[Identity library .NET]: /dotnet/api/overview/azure/identity-readme?view=azure-dotnet
400+
[defaultazurecredential.NET]: /dotnet/api/overview/azure/identity-readme?view=azure-dotnet#defaultazurecredential
401+
[NuGet]: https://www.nuget.org/
-1.54 KB
Loading

articles/azure-maps/rest-sdk-developer-guide.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ Azure Maps Python SDK supports Python version 3.7 or later. Check the [Azure S
4646

4747
Azure Maps JavaScript/TypeScript SDK supports LTS versions of [Node.js][Node.js] including versions in Active status and Maintenance status.
4848

49-
| Service Name  | NPM package  | Samples  |
49+
| Service Name  | npm package  | Samples  |
5050
|---------------|-------------------------|--------------|
5151
| [Search][js search readme] | [@azure/maps-search][js search package] | [search samples][js search sample] |
52-
| [Route][js route readme] | [@azure/maps-route][js route package] | [route samples][js route sample] |
52+
| [Route][js route readme] | [@azure-rest/maps-route][js route package] | [route samples][js route sample] |
5353

5454
<!--For more information, see the [JavaScript/TypeScript SDK Developers Guide](how-to-dev-guide-js-sdk.md).-->
5555

@@ -109,6 +109,10 @@ Azure Maps Java SDK supports [Java 8][Java 8] or above.
109109
[js route package]: https://www.npmjs.com/package/@azure-rest/maps-route
110110
[js route sample]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/maps/maps-route-rest/samples/v1-beta
111111

112+
[js route readme]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/maps/maps-route-rest/README.md
113+
[js route package]: https://www.npmjs.com/package/@azure-rest/maps-route
114+
[js route sample]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/maps/maps-route-rest/samples/v1-beta
115+
112116
<!-- Java SDK Developers Guide --->
113117
[Java 8]: https://www.java.com/en/download/java8_update.jsp
114118
[java search package]: https://repo1.maven.org/maven2/com/azure/azure-maps-search

0 commit comments

Comments
 (0)