Provides static methods for parsing AuthenticationHeaderValue
objects from strings.
The AuthenticationHeaderParser
class is a utility class that provides functionality to parse strings into AuthenticationHeaderValue
objects. This is particularly useful when processing HTTP headers that contain authentication information, such as the Authorization
header.
It offers methods to convert a string representation of an authentication header into a structured object, making it easier to work with the scheme and parameters of the authentication.
Name | Description |
---|---|
Parse(string value)
|
Parses a string into an AuthenticationHeaderValue object.
|
TryParse(string value, out AuthenticationHeaderValue parsedValue)
|
Attempts to parse a string into an AuthenticationHeaderValue object without throwing an exception if parsing fails.
|
When dealing with HTTP communication, the Authorization
header is crucial for authentication. The AuthenticationHeaderParser
class simplifies the process of extracting and interpreting the components of this header. For example, it can distinguish between the authentication scheme (e.g., "Basic", "Bearer") and the associated credentials or token.
The Parse
method is convenient for scenarios where you expect the input string to be valid. If the string is malformed, it will throw a FormatException
.
The TryParse
method provides a more robust approach, returning a boolean indicating success and providing the parsed value through an output parameter. This is ideal for situations where the input might be unreliable or when you want to handle parsing errors gracefully.
The AuthenticationHeaderValue
class itself holds the parsed scheme and parameters. AuthenticationHeaderParser
is the mechanism to obtain an instance of AuthenticationHeaderValue
from a raw string.
Parse
This example demonstrates how to use the Parse
method to create an AuthenticationHeaderValue
object from a Basic authentication string.
using System.Net.Http.Headers;
string headerString = "Basic YWxhZGRpbjpvcGVuc2VzYW1l";
AuthenticationHeaderValue authHeader = AuthenticationHeaderParser.Parse(headerString);
// authHeader.Scheme will be "Basic"
// authHeader.Parameter will be "YWxhZGRpbjpvcGVuc2VzYW1l"
TryParse
This example shows how to use the TryParse
method to safely parse a Bearer token string.
using System.Net.Http.Headers;
string headerString = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...";
AuthenticationHeaderValue parsedValue;
if (AuthenticationHeaderParser.TryParse(headerString, out parsedValue))
{
// Parsing succeeded
// parsedValue.Scheme will be "Bearer"
// parsedValue.Parameter will be "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
Console.WriteLine($"Scheme: {parsedValue.Scheme}, Parameter: {parsedValue.Parameter}");
}
else
{
// Parsing failed
Console.WriteLine("Failed to parse authentication header.");
}
TryParse
using System.Net.Http.Headers;
string invalidHeaderString = "InvalidScheme SomeCredentials";
AuthenticationHeaderValue parsedValue;
if (AuthenticationHeaderParser.TryParse(invalidHeaderString, out parsedValue))
{
// This block will not be executed for invalid input
Console.WriteLine($"Scheme: {parsedValue.Scheme}");
}
else
{
// This block will be executed
Console.WriteLine("Successfully handled invalid header string.");
}