Skip to content

Instantly share code, notes, and snippets.

@odahcam
Created October 10, 2018 12:24
Show Gist options
  • Save odahcam/1ec47edce421fed2f294275310b6146e to your computer and use it in GitHub Desktop.
Save odahcam/1ec47edce421fed2f294275310b6146e to your computer and use it in GitHub Desktop.
Method for creating a Basic Auth token for HTTP in C#.
using System;
using System.Text;
namespace MyNamespace
{
public class MyClass
{
/// <summary>
/// Creates an Authentication Token for "Basic Auth" HTTP Authorization.
/// </summary>
///
/// <param name="username"></param>
/// <param name="password"></param>
///
/// <returns></returns>
protected static string BasicAuthToken(string username, string password)
{
var encoded = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes($"{username}:{password}"));
return $"Basic {encoded}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment