AuthenticationChallengeReader Class

System.Net.Http.Headers.AuthenticationChallengeReader
Represents an authentication challenge.

Summary

The AuthenticationChallengeReader class provides a way to parse and access information from an HTTP WWW-Authenticate header, which typically contains authentication challenges.

Properties

Scheme

public string Scheme { get; }
Gets the authentication scheme (e.g., "Basic", "Bearer").

Parameters

public System.Collections.Generic.IReadOnlyDictionary<string, string> Parameters { get; }
Gets a read-only dictionary of parameters associated with the authentication challenge.

Constructors

AuthenticationChallengeReader

public AuthenticationChallengeReader(string challenge);
Initializes a new instance of the AuthenticationChallengeReader class with the specified challenge string.
public AuthenticationChallengeReader(string challenge)
{
    // Implementation details
}
                        

Methods

Parse

public static AuthenticationChallengeReader Parse(string challenge);
Parses a string representation of an authentication challenge and returns an AuthenticationChallengeReader object.
public static AuthenticationChallengeReader Parse(string challenge)
{
    // Implementation details
    return new AuthenticationChallengeReader(challenge);
}
                        

TryParse

public static bool TryParse(string challenge, out AuthenticationChallengeReader result);
Tries to parse a string representation of an authentication challenge and returns a value indicating whether the parsing succeeded.
public static bool TryParse(string challenge, [out] AuthenticationChallengeReader result)
{
    // Implementation details
    result = null;
    if (!string.IsNullOrWhiteSpace(challenge))
    {
        result = new AuthenticationChallengeReader(challenge);
        return true;
    }
    return false;
}
                        

Usage Example

Here's how you might use AuthenticationChallengeReader to parse a WWW-Authenticate header:

using System.Net.Http.Headers;

public class Example
{
    public static void Main(string[] args)
    {
        string wwwAuthenticateHeader = "Basic realm=\"MyServer\"";

        if (AuthenticationChallengeReader.TryParse(wwwAuthenticateHeader, out AuthenticationChallengeReader challenge))
        {
            Console.WriteLine($"Scheme: {challenge.Scheme}"); // Output: Scheme: Basic
            Console.WriteLine("Parameters:");
            foreach (var parameter in challenge.Parameters)
            {
                Console.WriteLine($"  {parameter.Key}: {parameter.Value}"); // Output:   realm: MyServer
            }
        }
        else
        {
            Console.WriteLine("Failed to parse WWW-Authenticate header.");
        }
    }
}