public static async Task <IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        [CosmosDB(
             databaseName: "VolcanoList",
             collectionName: "Volcano",
             ConnectionStringSetting = "CosmosDBConnection")] DocumentClient client,
        ExecutionContext context,
        ILogger log)
    {
        configuration  = GetAzureADConfiguration(context);
        tokenValidator = new TokenValidator(configuration, log, context);
        var claimsPrincipal = await tokenValidator.ValidateTokenAsync(req, audience);

        if (!tokenValidator.HasRightRolesAndScope(claimsPrincipal, scopeName, roles))
        {
            return(new UnauthorizedResult());
        }

        var searchterm = req.Query["volcanoname"];

        if (string.IsNullOrWhiteSpace(searchterm))
        {
            return((ActionResult) new NotFoundResult());
        }
        var result = await GetVolcanoData(client, searchterm);

        return(new OkObjectResult(result));
    }
Esempio n. 2
0
    public static async Task <IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
        [CosmosDB(
             databaseName: "VolcanoList",
             collectionName: "Volcano",
             ConnectionStringSetting = "CosmosDBConnection")] IAsyncCollector <Volcano> volcanoHandler,
        ExecutionContext context,
        ILogger log)
    {
        var config          = GetAzureADConfiguration(context);
        var tokenValidator  = new TokenValidator(config, log, context);
        var claimsPrincipal = await tokenValidator.ValidateTokenAsync(req, audience);

        if (!tokenValidator.HasRightRolesAndScope(claimsPrincipal, scopeName, roles))
        {
            return(new UnauthorizedResult());
        }
        var jsonString = await req.ReadAsStringAsync();

        var volcano = JsonConvert.DeserializeObject <Volcano>(jsonString);
        await volcanoHandler.AddAsync(volcano);

        return(new OkObjectResult(volcano));
    }