/// <summary> /// Extracts the username and password from an HTTP Basic authorized web header. /// </summary> /// <param name="headers">The incoming web headers.</param> /// <returns>The network credentials; or <c>null</c> if none could be discovered in the request.</returns> internal static NetworkCredential ParseHttpBasicAuth(System.Net.Http.Headers.HttpRequestHeaders headers) { Requires.NotNull(headers, "headers"); var authorizationHeader = headers.Authorization; if (authorizationHeader != null && string.Equals(authorizationHeader.Scheme, HttpBasicAuthScheme, StringComparison.Ordinal)) { string base64 = authorizationHeader.Parameter; byte[] bits = Convert.FromBase64String(base64); byte[] colonBits = Encoding.ASCII.GetBytes(new char[] { ':' }); // MUST result in 1 byte. int index = Array.FindIndex(bits, bite => (bite == colonBits[0])); if (index >= 0) { byte[] userNameBits = new byte[index]; byte[] passwordBits = new byte[bits.Length - (index + 1)]; Array.Copy(bits, 0, userNameBits, 0, userNameBits.Length); Array.Copy(bits, index + 1, passwordBits, 0, passwordBits.Length); string userName = Section231Decode(userNameBits); string password = Section231Decode(passwordBits); return(new NetworkCredential(userName, password)); } } return(null); }
/// <summary> /// Applies the HTTP Authorization header for HTTP Basic authentication. /// </summary> /// <param name="headers">The headers collection to set the authorization header to.</param> /// <param name="userName">The username. Cannot be empty.</param> /// <param name="password">The password. Cannot be null.</param> internal static void ApplyHttpBasicAuth(System.Net.Http.Headers.HttpRequestHeaders headers, string userName, string password) { Requires.NotNull(headers, "headers"); Requires.NotNullOrEmpty(userName, "userName"); Requires.NotNull(password, "password"); string concat = userName + ":" + password; byte[] bits = HttpBasicEncoding.GetBytes(concat); string base64 = Convert.ToBase64String(bits); headers.Authorization = new AuthenticationHeaderValue(HttpBasicAuthScheme, base64); }
/// <summary> /// Applies the HTTP Authorization header for HTTP Basic authentication. /// </summary> /// <param name="headers">The headers collection to set the authorization header to.</param> /// <param name="userName">The username. Cannot be empty.</param> /// <param name="password">The password. Cannot be null.</param> public static void ApplyHttpBasicAuth(System.Net.Http.Headers.HttpRequestHeaders headers, string userName, string password) { Requires.NotNull(headers, "headers"); Requires.NotNullOrEmpty(userName, "userName"); Requires.NotNull(password, "password"); byte[] userNameBits = Section231Encode(userName); byte[] passwordBits = Section231Encode(password); byte[] colonBits = Encoding.ASCII.GetBytes(new char[] { ':' }); byte[] concatBits = new byte[userNameBits.Length + colonBits.Length + passwordBits.Length]; userNameBits.CopyTo(concatBits, 0); colonBits.CopyTo(concatBits, userNameBits.Length); passwordBits.CopyTo(concatBits, userNameBits.Length + colonBits.Length); string base64 = Convert.ToBase64String(concatBits); headers.Authorization = new AuthenticationHeaderValue(HttpBasicAuthScheme, base64); }
/// <summary> /// Extracts the username and password from an HTTP Basic authorized web header. /// </summary> /// <param name="headers">The incoming web headers.</param> /// <returns>The network credentials; or <c>null</c> if none could be discovered in the request.</returns> internal static NetworkCredential ParseHttpBasicAuth(System.Net.Http.Headers.HttpRequestHeaders headers) { Requires.NotNull(headers, "headers"); var authorizationHeader = headers.Authorization; if (authorizationHeader != null && string.Equals(authorizationHeader.Scheme, HttpBasicAuthScheme, StringComparison.Ordinal)) { string base64 = authorizationHeader.Parameter; byte[] bits = Convert.FromBase64String(base64); string usernameColonPassword = HttpBasicEncoding.GetString(bits); string[] usernameAndPassword = usernameColonPassword.Split(ColonSeparator, 2); if (usernameAndPassword.Length == 2) { return(new NetworkCredential(usernameAndPassword[0], usernameAndPassword[1])); } } return(null); }