Skip to content

Commit 91e23d4

Browse files
committed
update the way to setup access token for dotnet face client
1 parent ec0e53e commit 91e23d4

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

articles/ai-services/computer-vision/how-to/identity-access-token.md

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -112,61 +112,51 @@ curl -X POST 'https://<client-endpoint>/face/v1.0/identify' \
112112
113113
#### [C#](#tab/csharp)
114114

115-
The following code snippets show you how to use an access token with the [Face SDK for C#](https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.Vision.Face).
115+
The following code snippets show you how to use an access token with the [Face SDK for C#](https://aka.ms/azsdk-csharp-face-pkg).
116116

117-
The following class uses an access token to create a **ServiceClientCredentials** object that can be used to authenticate a Face API client object. It automatically adds the access token as a header in every request that the Face client will make.
117+
The following class uses an access token to create a **HttpPipelineSynchronousPolicy** object that can be used to authenticate a Face API client object. It automatically adds the access token as a header in every request that the Face client will make.
118118

119119
```csharp
120-
public class LimitedAccessTokenWithApiKeyClientCredential : ServiceClientCredentials
120+
public class LimitedAccessTokenPolicy : HttpPipelineSynchronousPolicy
121121
{
122-
/// <summary>
123-
/// Creates a new instance of the LimitedAccessTokenWithApiKeyClientCredential class
124-
/// </summary>
125-
/// <param name="apiKey">API Key for the Face API or CognitiveService endpoint</param>
126-
/// <param name="limitedAccessToken">LimitedAccessToken to bypass the limited access program, requires ISV sponsership.</param>
127-
128-
public LimitedAccessTokenWithApiKeyClientCredential(string apiKey, string limitedAccessToken)
129-
{
130-
this.ApiKey = apiKey;
131-
this.LimitedAccessToken = limitedAccessToken;
122+
/// <summary>
123+
/// Creates a new instance of the LimitedAccessTokenPolicy class
124+
/// </summary>
125+
/// <param name="limitedAccessToken">LimitedAccessToken to bypass the limited access program, requires ISV sponsership.</param>
126+
public LimitedAccessTokenPolicy(string limitedAccessToken)
127+
{
128+
this.LimitedAccessToken = limitedAccessToken;
132129
}
133130

134-
private readonly string ApiKey;
135-
private readonly string LimitedAccesToken;
136-
137-
/// <summary>
138-
/// Add the Basic Authentication Header to each outgoing request
139-
/// </summary>
140-
/// <param name="request">The outgoing request</param>
141-
/// <param name="cancellationToken">A token to cancel the operation</param>
142-
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
143-
{
144-
if (request == null)
145-
throw new ArgumentNullException("request");
146-
request.Headers.Add("Ocp-Apim-Subscription-Key", ApiKey);
147-
request.Headers.Add("LimitedAccessToken", $"Bearer {LimitedAccesToken}");
148-
149-
return Task.FromResult<object>(null);
150-
}
151-
}
131+
private readonly string LimitedAccessToken;
132+
133+
/// <summary>
134+
/// Add the authentication header to each outgoing request
135+
/// </summary>
136+
/// <param name="message">The outgoing message</param>
137+
public override void OnSendingRequest(HttpMessage message)
138+
{
139+
message.Request.Headers.Add("LimitedAccessToken", $"Bearer {LimitedAccessToken}");
140+
}
141+
}
152142
```
153143

154144
In the client-side application, the helper class can be used like in this example:
155145

156146
```csharp
157-
static void Main(string[] args)
158-
{
147+
static void Main(string[] args)
148+
{
159149
// create Face client object
160-
var faceClient = new FaceClient(new LimitedAccessTokenWithApiKeyClientCredential(apiKey: "<client-face-key>", limitedAccessToken: "<token>"));
161-
162-
faceClient.Endpoint = "https://mytest-eastus2.cognitiveservices.azure.com";
150+
var clientOptions = new AzureAIVisionFaceClientOptions();
151+
clientOptions.AddPolicy(new LimitedAccessTokenPolicy("<token>"), HttpPipelinePosition.PerCall);
152+
FaceClient faceClient = new FaceClient(new Uri("<client-endpoint>"), new AzureKeyCredential("<client-face-key>"), clientOptions);
163153

164154
// use Face client in an API call
165-
using (var stream = File.OpenRead("photo.jpg"))
155+
using (var stream = File.OpenRead("photo.jpg"))
166156
{
167-
var result = faceClient.Face.DetectWithStreamAsync(stream, detectionModel: "Detection_03", recognitionModel: "Recognition_04", returnFaceId: true).Result;
157+
var response = faceClient.DetectAsync(BinaryData.FromStream(stream), FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, returnFaceId: true).Result;
168158

169-
Console.WriteLine(JsonConvert.SerializeObject(result));
159+
Console.WriteLine(JsonConvert.SerializeObject(response.Value));
170160
}
171161
}
172162
```

0 commit comments

Comments
 (0)