Skip to content

Instantly share code, notes, and snippets.

@noriyukitakei
Created February 7, 2019 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noriyukitakei/5a53161a8f18d69308c11ddd825cc886 to your computer and use it in GitHub Desktop.
Save noriyukitakei/5a53161a8f18d69308c11ddd825cc886 to your computer and use it in GitHub Desktop.
Azure FunctionsでToken Bindingを使って、Azure Active DirectoryによるOAuth認証をラクチンにする
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace hoge
{
public static class Function
{
[FunctionName("Function")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
[Token(IdentityProvider = "AAD", Resource = "https://graph.microsoft.com/", Identity = TokenIdentityMode.ClientCredentials)]string graphToken,
ILogger log)
{
log.LogInformation("Access Token:" + graphToken);
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment