Example #1
0
        /// <summary>
        /// Gets an instance of <see cref="IPartnerCredentials"/> used to access the Partner Center API.
        /// </summary>
        /// <param name="authority">Address of the authority to issue the token.</param>
        /// <returns>
        /// An instance of <see cref="IPartnerCredentials" /> that represents the access token.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="authority"/> is empty or null.
        /// </exception>
        /// <remarks>
        /// This function will use app plus user authentication to obtain the credentials.
        /// </remarks>
        public async Task <IPartnerCredentials> GetPartnerCenterAppPlusUserCredentialsAsync(string authority)
        {
            authority.AssertNotEmpty(nameof(authority));

            string key = $"Resource::PartnerCenter::{ClaimsPrincipal.Current.Identities.First().FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value}";

            IPartnerCredentials credentials =
                await this.service.Cache.FetchAsync <PartnerCenterTokenModel>(
                    CacheDatabaseType.Authentication, key);

            if (credentials != null && !credentials.IsExpired())
            {
                return(credentials);
            }

            AuthenticationToken token = await this.GetAppPlusUserTokenAsync(
                authority,
                this.service.Configuration.PartnerCenterEndpoint);

            credentials = await PartnerCredentials.Instance.GenerateByUserCredentialsAsync(
                this.service.Configuration.PartnerCenterApplicationId, token);

            await this.service.Cache.StoreAsync(
                CacheDatabaseType.Authentication, key, credentials);

            return(credentials);
        }
        /// <summary>
        /// Gets an instance of <see cref="IPartnerCredentials"/> used to access the Partner Center Managed API.
        /// </summary>
        /// <param name="authority">Address of the authority to issue the token.</param>
        /// <returns>
        /// An instance of <see cref="IPartnerCredentials" /> that represents the access token.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="authority"/> is empty or null.
        /// </exception>
        /// <remarks>This function will use app only authentication to obtain the credentials.</remarks>
        private async Task <IPartnerCredentials> GetPartnerCenterAppOnlyCredentialsAsync(string authority)
        {
            authority.AssertNotEmpty(nameof(authority));

            // Attempt to obtain the Partner Center token from the cache.
            IPartnerCredentials credentials =
                await service.Cache.FetchAsync <PartnerCenterTokenModel>(
                    CacheDatabaseType.Authentication, PartnerCenterCacheKey);

            if (credentials != null && !credentials.IsExpired())
            {
                return(credentials);
            }

            // The access token has expired, so a new one must be requested.
            credentials = await PartnerCredentials.Instance.GenerateByApplicationCredentialsAsync(
                service.Configuration.PartnerCenterApplicationId,
                service.Configuration.PartnerCenterApplicationSecret,
                service.Configuration.PartnerCenterApplicationTenantId);

            await service.Cache.StoreAsync(
                CacheDatabaseType.Authentication, PartnerCenterCacheKey, credentials);

            return(credentials);
        }
        /// <summary>
        /// Gets an instance of <see cref="IPartnerCredentials"/> used to access the Partner Center Managed API.
        /// </summary>
        /// <param name="authority">Address of the authority to issue the token.</param>
        /// <returns>
        /// An instance of <see cref="IPartnerCredentials" /> that represents the access token.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="authority"/> is empty or null.
        /// </exception>
        private async Task <IPartnerCredentials> GetPartnerCenterCredentialsAsync()
        {
            // Attempt to obtain the Partner Center token from the cache.
            IPartnerCredentials credentials =
                await provider.Cache.FetchAsync <Models.PartnerCenterToken>(
                    CacheDatabaseType.Authentication, PartnerCenterCacheKey).ConfigureAwait(false);

            if (credentials != null && !credentials.IsExpired())
            {
                return(credentials);
            }

            // The access token has expired, so a new one must be requested.
            credentials = await PartnerCredentials.Instance.GenerateByApplicationCredentialsAsync(
                provider.Configuration.PartnerCenterApplicationId,
                provider.Configuration.PartnerCenterApplicationSecret.ToUnsecureString(),
                provider.Configuration.PartnerCenterAccountId).ConfigureAwait(false);

            await provider.Cache
            .StoreAsync(CacheDatabaseType.Authentication, PartnerCenterCacheKey, credentials).ConfigureAwait(false);

            return(credentials);
        }