Basic Authentication

The Location Hub® Viewer API utilizes Basic Authentication for authorization and access of the services. Basic Authentication sends a Base64 encoded string that contains a user name and password for the client via HTTP headers. Base64 is not a form of encryption and should be considered the same as sending the user name and password in clear text. However, all traffic is encrypted and transmitted over a TLS v1.2 (Transport Layer Security) connection, and therefore the user name and password is protected during transmission.

The user credentials are sent using the HTTP Authorization header. The header is constructed as follows:

  • Concatenate username and password into a string with a colon between as shown: “username:password”
  • Encode the resulting string in a Base64 variant
  • Prefix the encoded string with “Basic ”; the space after Basic is required

These encoded credentials are then sent in the HTTP Authorization header with every Location Hub Viewer API request.

 

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
 
namespace BasicAuthenticationTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string requestURL = "https://the.service.url/v1/";
            string serviceMethod = "GetRequest";
            string username = "YourUserName";
            string password = "YourPassword!";
 
            Console.Out.WriteLine();
            Console.Out.WriteLine("Request Url: " + requestURL);
 
            WebRequest request = WebRequest.Create(requestURL + serviceMethod);
            WebResponse response = null;
            try
            {
                if (username != null)
                {
                    string authInfo = username + ":" + password;
                    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
                    Console.Out.WriteLine("Base64 Encoded String: " + authInfo);
                    request.Headers["Authorization"] = "Basic " + authInfo;
                }
 
                response = request.GetResponse();
 
                Console.Out.WriteLine("Response Header:");
                foreach (var header in response.Headers)
                {
                    Console.Out.WriteLine("t" + header + " = " + response.Headers[header.ToString()]);
                }
 
            }
            catch (WebException ex)
            {
                Console.Out.WriteLine("HTTP Status: " + ex.Message);
            }
            finally
            {
                if (response != null)
                {
                    Console.Out.WriteLine("HTTP Status: " + ((HttpWebResponse)response).StatusCode);
                    response.Close();
                }
                Console.ReadLine();
            }
        }
    }
}