AuthenticationHeaderValue Class

System.Net.Http.Headers

Represents an authentication scheme and its corresponding credentials.

Syntax

C#

                    public sealed class AuthenticationHeaderValue : ICloneable
                

Visual Basic

                    Public NotInheritable Class AuthenticationHeaderValue Implements ICloneable
                
Constructors

AuthenticationHeaderValue(string scheme)

Initializes a new instance of the AuthenticationHeaderValue class with the specified authentication scheme.

scheme
The authentication scheme. For example, "Basic" or "Bearer".
                    public AuthenticationHeaderValue(string scheme)
                

AuthenticationHeaderValue(string scheme, string parameter)

Initializes a new instance of the AuthenticationHeaderValue class with the specified authentication scheme and parameter.

scheme
The authentication scheme. For example, "Basic" or "Bearer".
parameter
The authentication parameter. For example, a base64-encoded username:password for Basic, or a token for Bearer.
                    public AuthenticationHeaderValue(string scheme, string parameter)
                
Properties
Name Description
Parameter Gets the authentication parameter.
Scheme Gets the authentication scheme.
Methods
Name Description
ToString() Returns a string representation of the current object.
Clone() Creates a new object that is a copy of the current instance.
Equals(object obj) Determines whether the specified object is equal to the current object.
GetHashCode() Serves as the default hash function.
Fields

This class has no fields.

Examples

Basic Authentication

                using System.Net.Http;
using System.Net.Http.Headers;

// ...

var request = new HttpRequestMessage(HttpMethod.Get, "https://api.example.com/data");

// Basic authentication with username and password
string username = "myuser";
string password = "mypassword";
string credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}"));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);

// Send the request
// ...

            

Bearer Token Authentication

                using System.Net.Http;
using System.Net.Http.Headers;

// ...

var request = new HttpRequestMessage(HttpMethod.Get, "https://api.example.com/protected");

// Bearer token authentication
string bearerToken = "YOUR_ACCESS_TOKEN";
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);

// Send the request
// ...