Created
December 9, 2019 01:18
-
-
Save arafattehsin/2e83ae730dbe1556a63d470c9a1caa1a to your computer and use it in GitHub Desktop.
This is a sample Controller for obtaining a token for Immersive Reader Cognitive Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Logging; | |
using ImmersiveReaderDemo.Models; | |
using Microsoft.IdentityModel.Clients.ActiveDirectory; | |
namespace ImmersiveReaderDemo.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
private readonly ILogger<HomeController> _logger; | |
private readonly string TenantId; // Azure subscription TenantId | |
private readonly string ClientId; // Azure AD ApplicationId | |
private readonly string ClientSecret; // Azure AD Application Service Principal Client Secret | |
private readonly string Subdomain; // Immersive Reader resource subdomain (resource 'Name' if the resource was created in the Azure portal, or 'CustomSubDomain' option if the resource was created with Azure CLI Powershell. Check the Azure portal for the subdomain on the Endpoint in the resource Overview page, for example, 'https://[SUBDOMAIN].cognitiveservices.azure.com/') | |
public HomeController(ILogger<HomeController> logger, Microsoft.Extensions.Configuration.IConfiguration configuration) | |
{ | |
_logger = logger; | |
TenantId = configuration["TenantId"]; | |
ClientId = configuration["ClientId"]; | |
ClientSecret = configuration["ClientSecret"]; | |
Subdomain = configuration["Subdomain"]; | |
} | |
public IActionResult Index() | |
{ | |
return View(); | |
} | |
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | |
public IActionResult Error() | |
{ | |
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); | |
} | |
[Route("subdomain")] | |
public string GetSubdomain() | |
{ | |
return Subdomain; | |
} | |
[Route("token")] | |
public async Task<string> GetToken() | |
{ | |
return await GetTokenAsync(); | |
} | |
/// <summary> | |
/// Get an Azure AD authentication token | |
/// </summary> | |
private async Task<string> GetTokenAsync() | |
{ | |
string authority = $"https://login.windows.net/{TenantId}"; | |
const string resource = "https://cognitiveservices.azure.com/"; | |
AuthenticationContext authContext = new AuthenticationContext(authority); | |
ClientCredential clientCredential = new ClientCredential(ClientId, ClientSecret); | |
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resource, clientCredential); | |
return authResult.AccessToken; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment