AuthenticationHeaderParser Class

Namespace: System.Net.Http.Headers

Provides classes that allow you to work with HTTP headers.

Summary

A parser for HTTP authentication headers.

Remarks

This class is not intended for direct use by application developers. It is used internally by the .NET Framework to parse and manage HTTP authentication headers.

Members

Name Description
Parse

Parses a string representing an authentication header and returns an AuthenticationHeaderValue object.

public static System.Net.Http.Headers.AuthenticationHeaderValue Parse(string s)

Parameters:

  • s: The string to parse.

Returns: An AuthenticationHeaderValue object.

Example:

using System.Net.Http.Headers; public static class Example { public static void Main() { string authHeader = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="; try { AuthenticationHeaderValue authValue = AuthenticationHeaderParser.Parse(authHeader); Console.WriteLine($"Scheme: {authValue.Scheme}"); Console.WriteLine($"Parameter: {authValue.Parameter}"); } catch (FormatException e) { Console.WriteLine($"Error parsing header: {e.Message}"); } } }
TryParse

Tries to parse a string representing an authentication header and returns a boolean indicating success.

public static bool TryParse(string s, [out] out System.Net.Http.Headers.AuthenticationHeaderValue value)

Parameters:

  • s: The string to parse.
  • value: When this method returns, contains the parsed AuthenticationHeaderValue object, if the parsing succeeded, or null otherwise.

Returns: true if the string was parsed successfully; otherwise, false.

Example:

using System.Net.Http.Headers; public static class Example { public static void Main() { string authHeader = "Bearer xyz123"; AuthenticationHeaderValue authValue; if (AuthenticationHeaderParser.TryParse(authHeader, out authValue)) { Console.WriteLine($"Successfully parsed: Scheme={authValue.Scheme}, Parameter={authValue.Parameter}"); } else { Console.WriteLine("Failed to parse authentication header."); } } }

See Also