Click or drag to resize
IAuthorizationProvider Interface
Interface that provides authorization for requests to the Microsoft Speech Service. Service peers are required to provide an implementation of this class to make connections to the Speech Service.

Namespace:  Microsoft.Bing.Speech
Assembly:  Microsoft.Bing.Speech (in Microsoft.Bing.Speech.dll) Version: 2.0.1.0 (2.0.1)
Syntax
public interface IAuthorizationProvider

The IAuthorizationProvider type exposes the following members.

Methods
  NameDescription
Public methodGetAuthorizationTokenAsync
Gets the authorization token asynchronously.
Top
Examples
This is a sample implementation that uses Cognitive Services authorization.

For more information, refer to the Token API V1.0

/// <summary>
/// Cognitive Services Authorization Provider.
/// </summary>
public sealed class CognitiveServicesAuthorizationProvider : IAuthorizationProvider
{
    /// <summary>
    /// The fetch token URI
    /// </summary>
    private const string FetchTokenUri = "https://api.cognitive.microsoft.com/sts/v1.0";

    /// <summary>
    /// The subscription key
    /// </summary>
    private readonly string subscriptionKey;

    /// <summary>
    /// Initializes a new instance of the <see cref="CognitiveServicesAuthorizationProvider" /> class.
    /// </summary>
    /// <param name="subscriptionKey">The subscription identifier.</param>
    public CognitiveServicesAuthorizationProvider(string subscriptionKey)
    {
        if (subscriptionKey == null)
        {
            throw new ArgumentNullException(nameof(subscriptionKey));
        }

        if (string.IsNullOrWhiteSpace(subscriptionKey))
        {
            throw new ArgumentException(nameof(subscriptionKey));
        }

        this.subscriptionKey = subscriptionKey;
    }

    /// <summary>
    /// Gets the authorization token asynchronously.
    /// </summary>
    /// <returns>
    /// A task that represents the asynchronous read operation. The value of the string parameter contains the next the authorization token.
    /// </returns>
    /// <remarks>
    /// This method should always return a valid authorization token at the time it is called.
    /// </remarks>
    public Task<string> GetAuthorizationTokenAsync()
    {
        // Use token caching instead of returning a new token every time this is called.
        return FetchToken(FetchTokenUri, this.subscriptionKey);
    }

    /// <summary>
    /// Fetches the token.
    /// </summary>
    /// <param name="fetchUri">The fetch URI.</param>
    /// <param name="subscriptionKey">The subscription key.</param>
    /// <returns>An access token.</returns>
    private static async Task<string> FetchToken(string fetchUri, string subscriptionKey)
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
            var uriBuilder = new UriBuilder(fetchUri);
            uriBuilder.Path += "/issueToken";

            using (var result = await client.PostAsync(uriBuilder.Uri.AbsoluteUri, null).ConfigureAwait(false))
            {
                return await result.Content.ReadAsStringAsync().ConfigureAwait(false);
            }
        }
    }
}
See Also