/// <summary>
        /// Initializes a new instance of the <see cref="ManagedIdentityTokenSource"/> class.
        /// </summary>
        /// <param name="resource">
        /// The Azure Active Directory resource identifier of the web API being invoked.
        /// For example, <c>https://management.core.windows.net/.default</c> or <c>https://graph.microsoft.com/.default</c>.
        /// </param>
        /// <param name="options">Optional Azure credential options to use when authenticating.</param>
        public ManagedIdentityTokenSource(string resource, ManagedIdentityOptions options = null)
        {
            this.Resource = resource ?? throw new ArgumentNullException(nameof(resource));
            this.Options  = options;

            if (this.Resource.Equals("https://management.core.windows.net") || this.Resource.Equals("https://management.core.windows.net/"))
            {
                this.Resource = "https://management.core.windows.net/.default";
            }
            else if (this.Resource.Equals("https://graph.microsoft.com") || this.Resource.Equals("https://graph.microsoft.com/"))
            {
                this.Resource = "https://graph.microsoft.com/.default";
            }
        }
コード例 #2
0
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                var safeTokenSerializer = GetTokenSourceSerializer(serializer);

                JToken json = JToken.ReadFrom(reader);

                if (json.Type == JTokenType.Null)
                {
                    return(null);
                }

                JObject jsonObject = (JObject)json;

                if (jsonObject.TryGetValue("kind", out JToken kindValue))
                {
                    if (Enum.TryParse((string)kindValue, out TokenSourceType tokenSourceKind) &&
                        tokenSourceKind == TokenSourceType.AzureManagedIdentity)
                    {
                        string resourceString = (string)jsonObject.GetValue("resource", StringComparison.Ordinal);

                        if (jsonObject.TryGetValue("options", out JToken optionsToken))
                        {
                            ManagedIdentityOptions managedIdentityOptions = optionsToken.ToObject <JObject>().ToObject <ManagedIdentityOptions>();
                            return(new ManagedIdentityTokenSource(resourceString, managedIdentityOptions));
                        }

                        return(new ManagedIdentityTokenSource(resourceString));
                    }

                    throw new NotSupportedException($"The token source kind '{kindValue.ToString(Formatting.None)}' is not supported.");
                }
                else if (jsonObject.TryGetValue("$type", StringComparison.Ordinal, out JToken clrTypeValue))
                {
                    Type runtimeType = Type.GetType((string)clrTypeValue, throwOnError: true);
                    return(jsonObject.ToObject(runtimeType, safeTokenSerializer));
                }
                else
                {
                    // Don't know how to deserialize this - use default behavior (this may fail)
                    return(jsonObject.ToObject(objectType));
                }
            }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ManagedIdentityTokenSource"/> class.
 /// </summary>
 /// <param name="resource">
 /// The Azure Active Directory resource identifier of the web API being invoked.
 /// For example, <c>https://management.core.windows.net/</c> or <c>https://graph.microsoft.com/</c>.
 /// </param>
 /// <param name="options">Optional Azure credential options to use when authenticating.</param>
 public ManagedIdentityTokenSource(string resource, ManagedIdentityOptions options = null)
 {
     this.Resource = resource ?? throw new ArgumentNullException(nameof(resource));
     this.Options  = options;
 }