| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.ComponentModel; |
| | 5 | | using System.Threading; |
| | 6 | | using Azure.Core; |
| | 7 | |
|
| | 8 | | namespace Azure |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Key credential used to authenticate to an Azure Service. |
| | 12 | | /// It provides the ability to update the key without creating a new client. |
| | 13 | | /// </summary> |
| | 14 | | public class AzureKeyCredential |
| | 15 | | { |
| | 16 | | private string _key; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Key used to authenticate to an Azure service. |
| | 20 | | /// </summary> |
| | 21 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 22 | | public string Key |
| | 23 | | { |
| 6 | 24 | | get => Volatile.Read(ref _key); |
| 4 | 25 | | private set => Volatile.Write(ref _key, value); |
| | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Initializes a new instance of the <see cref="AzureKeyCredential"/> class. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="key">Key to use to authenticate with the Azure service.</param> |
| | 32 | | /// <exception cref="System.ArgumentNullException"> |
| | 33 | | /// Thrown when the <paramref name="key"/> is null. |
| | 34 | | /// </exception> |
| | 35 | | /// <exception cref="System.ArgumentException"> |
| | 36 | | /// Thrown when the <paramref name="key"/> is empty. |
| | 37 | | /// </exception> |
| | 38 | | #pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable. |
| 8 | 39 | | public AzureKeyCredential(string key) => Update(key); |
| | 40 | | #pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable. |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Updates the service key. |
| | 44 | | /// This is intended to be used when you've regenerated your service key |
| | 45 | | /// and want to update long lived clients. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="key">Key to authenticate the service against.</param> |
| | 48 | | /// <exception cref="System.ArgumentNullException"> |
| | 49 | | /// Thrown when the <paramref name="key"/> is null. |
| | 50 | | /// </exception> |
| | 51 | | /// <exception cref="System.ArgumentException"> |
| | 52 | | /// Thrown when the <paramref name="key"/> is empty. |
| | 53 | | /// </exception> |
| | 54 | | public void Update(string key) |
| | 55 | | { |
| 4 | 56 | | Argument.AssertNotNullOrEmpty(key, nameof(key)); |
| 4 | 57 | | Key = key; |
| 4 | 58 | | } |
| | 59 | | } |
| | 60 | | } |