Created
January 6, 2018 20:30
-
-
Save ThomasPe/7389654450c5e30460e44508b395cc1f to your computer and use it in GitHub Desktop.
A C# Azure Function for calling webhooks with basic authentication
This file contains hidden or 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.Net; | |
[FunctionName("GetEnvironmentVariables")] | |
public static void Run(TimerInfo myTimer, TraceWriter log) | |
{ | |
log.Info($"C# Timer trigger function executed at: {DateTime.Now}"); | |
string username = GetEnvironmentVariable("Webhook_Username"); | |
string password = GetEnvironmentVariable("Webhook_Password"); | |
string webhook = "https://mywebhookurl.com"; | |
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(webhook); | |
string encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)); | |
req.Headers.Add("Authorization", "Basic " + encoded); | |
req.Method = "POST"; | |
req.ContentLength = 0; | |
var response = req.GetResponse(); | |
log.Info(response.ToString()); | |
} | |
public static string GetEnvironmentVariable(string name) | |
{ | |
return System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment