-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathAzureKeyCredentialPolicy.cs
More file actions
50 lines (45 loc) · 1.9 KB
/
AzureKeyCredentialPolicy.cs
File metadata and controls
50 lines (45 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using Azure.Core.Pipeline;
#nullable enable
namespace Azure.Core
{
internal class AzureKeyCredentialPolicy : HttpPipelineSynchronousPolicy
{
private readonly string _name;
private readonly AzureKeyCredential _credential;
private readonly string? _prefix;
/// <summary>
/// Initializes a new instance of the <see cref="AzureKeyCredentialPolicy"/> class.
/// </summary>
/// <param name="credential">The <see cref="AzureKeyCredential"/> used to authenticate requests.</param>
/// <param name="name">The name of the key header used for the credential.</param>
/// <param name="prefix">The prefix to apply before the credential key. For example, a prefix of "SharedAccessKey" would result in
/// a value of "SharedAccessKey {credential.Key}" being stamped on the request header with header key of <paramref name="name"/>.</param>
public AzureKeyCredentialPolicy(AzureKeyCredential credential, string name, string? prefix = null)
{
if (credential is null)
{
throw new ArgumentNullException(nameof(credential));
}
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}
if (name.Length == 0)
{
throw new ArgumentException("Value cannot be an empty string.", nameof(name));
}
_credential = credential;
_name = name;
_prefix = prefix;
}
/// <inheritdoc/>
public override void OnSendingRequest(HttpMessage message)
{
base.OnSendingRequest(message);
message.Request.Headers.SetValue(_name, _prefix != null ? $"{_prefix} {_credential.Key}" : _credential.Key);
}
}
}