Created
February 7, 2019 13:23
-
-
Save noriyukitakei/5a53161a8f18d69308c11ddd825cc886 to your computer and use it in GitHub Desktop.
Azure FunctionsでToken Bindingを使って、Azure Active DirectoryによるOAuth認証をラクチンにする
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.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