/// <summary>
        /// Add claims to the request context of the given request message
        /// </summary>
        /// <param name="wwwAuthenticateHeader">String representation of www Authenticate Header</param>
        /// <param name="newRequest">Request message to add claims to</param>
        private void AddClaimsToRequestContext(HttpRequestMessage newRequest, string wwwAuthenticateHeader)
        {
            int claimsStart = wwwAuthenticateHeader.IndexOf("claims=", StringComparison.OrdinalIgnoreCase);

            if (claimsStart < 0)
            {
                return;       // do nothing as there is no claims in www Authenticate Header
            }
            claimsStart += 8; // jump to the index after the opening quotation mark

            // extract and decode the Base 64 encoded claims property
            byte[] bytes           = Convert.FromBase64String(wwwAuthenticateHeader.Substring(claimsStart, wwwAuthenticateHeader.Length - claimsStart - 1));
            string claimsChallenge = Encoding.UTF8.GetString(bytes, 0, bytes.Length);

            // Try to get the current options otherwise create new ones
            AuthenticationHandlerOption   authenticationHandlerOption  = newRequest.GetMiddlewareOption <AuthenticationHandlerOption>() ?? AuthOption;
            IAuthenticationProviderOption authenticationProviderOption = authenticationHandlerOption.AuthenticationProviderOption ?? new CaeAuthenticationProviderOption();

            // make sure that there is no information loss due to casting by copying over the scopes information if necessary
            CaeAuthenticationProviderOption caeAuthenticationProviderOption;

            if (authenticationProviderOption is CaeAuthenticationProviderOption option)
            {
                caeAuthenticationProviderOption = option;
            }
            else
            {
                caeAuthenticationProviderOption = new CaeAuthenticationProviderOption(authenticationProviderOption);
            }

            // update the claims property in the options
            caeAuthenticationProviderOption.Claims = claimsChallenge;
            authenticationHandlerOption.AuthenticationProviderOption = caeAuthenticationProviderOption;

            // update the request context with the updated options
            GraphRequestContext requestContext = newRequest.GetRequestContext();

            requestContext.MiddlewareOptions[typeof(AuthenticationHandlerOption).ToString()] = authenticationHandlerOption;
            newRequest.Properties[typeof(GraphRequestContext).ToString()] = requestContext;
        }
 /// <summary>
 /// Construct a new <see cref="AuthenticationHandler"/>
 /// </summary>
 /// <param name="authenticationProvider">An authentication provider to pass to <see cref="AuthenticationHandler"/> for authenticating requests.</param>
 /// <param name="innerHandler">A HTTP message handler to pass to the <see cref="AuthenticationHandler"/> for sending requests.</param>
 /// <param name="authOption">An OPTIONAL <see cref="Microsoft.Graph.AuthenticationHandlerOption"/> to configure <see cref="AuthenticationHandler"/></param>
 public AuthenticationHandler(IAuthenticationProvider authenticationProvider, HttpMessageHandler innerHandler, AuthenticationHandlerOption authOption = null)
     : this(authenticationProvider, authOption)
 {
     InnerHandler           = innerHandler;
     AuthenticationProvider = authenticationProvider;
 }
 /// <summary>
 /// Construct a new <see cref="AuthenticationHandler"/>
 /// <param name="authenticationProvider">An authentication provider to pass to <see cref="AuthenticationHandler"/> for authenticating requests.</param>
 /// </summary>
 /// <param name="authOption">An OPTIONAL <see cref="Microsoft.Graph.AuthenticationHandlerOption"/> to configure <see cref="AuthenticationHandler"/></param>
 public AuthenticationHandler(IAuthenticationProvider authenticationProvider, AuthenticationHandlerOption authOption = null)
 {
     AuthenticationProvider = authenticationProvider;
     AuthOption             = authOption ?? new AuthenticationHandlerOption();
 }