<PackageReference Include="System.ClientModel" Version="1.1.0" />

ApiKeyAuthenticationPolicy

A PipelinePolicy that uses an ApiKeyCredential to set a value on a PipelineRequest to authenticate with the cloud service.
using System.ClientModel.Internal; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace System.ClientModel.Primitives { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class ApiKeyAuthenticationPolicy : PipelinePolicy { [System.Runtime.CompilerServices.NullableContext(0)] private enum KeyLocation { Header } private readonly string _name; [System.Runtime.CompilerServices.Nullable(2)] private readonly string _keyPrefix; private readonly KeyLocation _location; private readonly ApiKeyCredential _credential; public static ApiKeyAuthenticationPolicy CreateHeaderApiKeyPolicy(ApiKeyCredential credential, string headerName, [System.Runtime.CompilerServices.Nullable(2)] string keyPrefix = null) { Argument.AssertNotNull(credential, "credential"); Argument.AssertNotNullOrEmpty(headerName, "headerName"); return new ApiKeyAuthenticationPolicy(credential, headerName, KeyLocation.Header, keyPrefix); } public static ApiKeyAuthenticationPolicy CreateBasicAuthorizationPolicy(ApiKeyCredential credential) { Argument.AssertNotNull(credential, "credential"); return new ApiKeyAuthenticationPolicy(credential, "Authorization", KeyLocation.Header, "Basic"); } public static ApiKeyAuthenticationPolicy CreateBearerAuthorizationPolicy(ApiKeyCredential credential) { Argument.AssertNotNull(credential, "credential"); return new ApiKeyAuthenticationPolicy(credential, "Authorization", KeyLocation.Header, "Bearer"); } private ApiKeyAuthenticationPolicy(ApiKeyCredential credential, string name, KeyLocation keyLocation, [System.Runtime.CompilerServices.Nullable(2)] string keyPrefix = null) { Argument.AssertNotNull(credential, "credential"); Argument.AssertNotNullOrEmpty(name, "name"); _credential = credential; _name = name; _location = keyLocation; _keyPrefix = keyPrefix; } public sealed override void Process(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex) { SetKey(message); PipelinePolicy.ProcessNext(message, pipeline, currentIndex); } [AsyncStateMachine(typeof(<ProcessAsync>d__9))] public sealed override ValueTask ProcessAsync(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex) { <ProcessAsync>d__9 stateMachine = default(<ProcessAsync>d__9); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder.Create(); stateMachine.<>4__this = this; stateMachine.message = message; stateMachine.pipeline = pipeline; stateMachine.currentIndex = currentIndex; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } private void SetKey(PipelineMessage message) { if (_location == KeyLocation.Header) { SetHeader(message); return; } throw new InvalidOperationException($"""{_location}"""); } private void SetHeader(PipelineMessage message) { _credential.Deconstruct(out string key); message.Request.Headers.Set(_name, (_keyPrefix != null) ? (_keyPrefix + " " + key) : key); } } }