#r "Newtonsoft.Json" #r "System.Text.Json" using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using Newtonsoft.Json; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using System.Text.Json; using System.Threading.Tasks; using System.Globalization; using System.Text.Json.Serialization; //using System.Exception; public class Repository { [JsonPropertyName("name")] public string Name { get; set; } [JsonPropertyName("description")] public string Description { get; set; } [JsonPropertyName("html_url")] public Uri GitHubHomeUrl { get; set; } [JsonPropertyName("homepage")] public Uri Homepage { get; set; } [JsonPropertyName("watchers")] public int Watchers { get; set; } [JsonPropertyName("pushed_at")] public string JsonDate { get; set; } public DateTime LastPush => DateTime.ParseExact(JsonDate, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture); } public static async Task<IActionResult> Run(HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string name = req.Query["name"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; string responseMessage = string.IsNullOrEmpty(name) ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response." : $"Hello, {name}. This HTTP triggered function executed successfully."; // Testing this...disregard for demo // ReadTextFile(); log.LogInformation("Performing ProcessRepositories code section..."); var repositories = await ProcessRepositories(); foreach (var repo in repositories) { if (repo == null) { continue; } log.LogInformation($"Repo Name: {repo.Name}"); log.LogInformation($"Repo Description: {repo.Description}"); } return new OkObjectResult(responseMessage); } // Ref URL: https://github.com/dotnet/samples/blob/master/csharp/getting-started/console-webapiclient/Program.cs private static async Task<List<Repository>> ProcessRepositories() { var client = new HttpClient(); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json") ); client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter"); var streamTask = client.GetStreamAsync("https://api.github.com/orgs/dotnet/repos"); var repositories = await System.Text.Json.JsonSerializer.DeserializeAsync<List<Repository>>(await streamTask); return repositories; } // Ref URL - https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-read-from-a-text-file private void ReadTextFile(ILogger log) { try { // Example #1 // Read the file as one string. string text = System.IO.File.ReadAllText(@"\\10.10.2.5\test\testfile.txt"); // Display the file contents to the console. Variable text is a string. log.LogInformation("Contents of WriteText.txt = {0}", text); } catch(Exception ex) { log.LogInformation("Exception = {0}", ex.Message); } }
Run the HTTP Trigger and see the Function run successfully
Set the application rule to Deny, and run the Azure Function. You will see the Function will respond with an error in the output window. This is because the Firewall is blocking the traffic via the outbound public static IP.
Now set the Application Rule in the Azurefirewall to Allow and run the Azure Function. The call is now successful, retrieving the GitHub repositories per the logic in the HTTP Trigger function, this is viewable in the output window.
The public static outbound IP address is Firewall public IP, in this case 52.252.28.12. This is the IP to be shared with the Vendors or 3rd Party Software providers such that it can be whitelisted and calls can be made to the function via this public static IP.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.