< Summary

Class:Azure.Identity.ManagedIdentityCredential
Assembly:Azure.Identity
File(s):C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\ManagedIdentityCredential.cs
Covered lines:19
Uncovered lines:0
Coverable lines:19
Total lines:100
Line coverage:100% (19 of 19)
Covered branches:2
Total branches:2
Branch coverage:100% (2 of 2)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
GetTokenAsync()-100%100%
GetToken(...)-100%100%
GetTokenImplAsync()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\ManagedIdentityCredential.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using Azure.Core;
 5using Azure.Core.Diagnostics;
 6using System;
 7using System.IO;
 8using System.Text;
 9using System.Text.Json;
 10using System.Threading;
 11using System.Threading.Tasks;
 12using Azure.Core.Pipeline;
 13
 14namespace Azure.Identity
 15{
 16    /// <summary>
 17    /// Attempts authentication using a managed identity that has been assigned to the deployment environment. This auth
 18    /// App Service and Azure Functions applications, as well as the Azure Cloud Shell. More information about configuri
 19    /// https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview
 20    /// </summary>
 21    public class ManagedIdentityCredential : TokenCredential
 22    {
 23        internal const string MsiUnavailableError = "No managed identity endpoint found.";
 24
 25        private readonly CredentialPipeline _pipeline;
 26        private readonly ManagedIdentityClient _client;
 27
 28        /// <summary>
 29        /// Protected constructor for mocking.
 30        /// </summary>
 4031        protected ManagedIdentityCredential()
 32        {
 33
 4034        }
 35
 36        /// <summary>
 37        /// Creates an instance of the ManagedIdentityCredential capable of authenticating a resource with a managed ide
 38        /// </summary>
 39        /// <param name="clientId">
 40        /// The client id to authenticate for a user assigned managed identity.  More information on user assigned manag
 41        /// https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview#how-a-us
 42        /// </param>
 43        /// <param name="options">Options to configure the management of the requests sent to the Azure Active Directory
 44        public ManagedIdentityCredential(string clientId = null, TokenCredentialOptions options = null)
 1645            : this(clientId, CredentialPipeline.GetInstance(options))
 46        {
 1647        }
 48
 49        internal ManagedIdentityCredential(string clientId, CredentialPipeline pipeline)
 2250            : this(pipeline, new ManagedIdentityClient(pipeline, clientId))
 51        {
 2252        }
 53
 7054        internal ManagedIdentityCredential(CredentialPipeline pipeline, ManagedIdentityClient client)
 55        {
 56
 7057            _pipeline = pipeline;
 58
 7059            _client = client;
 7060        }
 61
 62        /// <summary>
 63        /// Obtains an <see cref="AccessToken"/> from the Managed Identity service if available. This method is called b
 64        /// </summary>
 65        /// <param name="requestContext">The details of the authentication request.</param>
 66        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 67        /// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls, or a default <
 68        public override async ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken
 69        {
 3270            return await GetTokenImplAsync(true, requestContext, cancellationToken).ConfigureAwait(false);
 1271        }
 72
 73        /// <summary>
 74        /// Obtains an <see cref="AccessToken"/> from the Managed Identity service if available. This method is called b
 75        /// </summary>
 76        /// <param name="requestContext">The details of the authentication request.</param>
 77        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 78        /// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls, or a default <
 79        public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken = d
 80        {
 3481            return GetTokenImplAsync(false, requestContext, cancellationToken).EnsureCompleted();
 82        }
 83
 84        private async ValueTask<AccessToken> GetTokenImplAsync(bool async, TokenRequestContext requestContext, Cancellat
 85        {
 6686            using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("ManagedIdentityCredential.GetToken", r
 87
 88            try
 89            {
 6690                AccessToken result = async ? await _client.AuthenticateAsync(requestContext.Scopes, cancellationToken).C
 91
 2492                return scope.Succeeded(result);
 93            }
 4294            catch (Exception e)
 95            {
 4296               throw scope.FailWrapAndThrow(e);
 97            }
 2498        }
 99    }
 100}