Skip to content

Instantly share code, notes, and snippets.

@akeller
Created October 1, 2020 21:13
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 akeller/fe838fc982e1f938acf6f1c4176d9dfa to your computer and use it in GitHub Desktop.
Save akeller/fe838fc982e1f938acf6f1c4176d9dfa to your computer and use it in GitHub Desktop.
Digest Auth Example C#
using System;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
namespace ConsoleApp1
{
class Program
{
private static readonly string user = "<USERNAME>";
private static readonly string password = "<PASSWORD>";
private static readonly string url = "http://localhost:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications";
private static async Task makeRequest(string url, string user, string password)
{
var credCache = new CredentialCache();
credCache.Add(new Uri(url), "Digest", new NetworkCredential(user, password));
var httpClient = new HttpClient(new HttpClientHandler { Credentials = credCache });
var response = await httpClient.GetAsync(url);
//var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine("response: \n" + response);
//Console.WriteLine("responseString: \n" + responseString);
}
static void Main(string[] args)
{
try
{
makeRequest(url, user, password).Wait();
}
catch (Exception ex)
{
Console.WriteLine("There was an exception: {ex.ToString()}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment