Beispiel #1
0
        /// <summary>
        /// Authenticates with the Partner Center APIs user.
        /// </summary>
        /// <returns>A Partner Center API client User.</returns>
        //private static async Task<IAggregatePartner> AcquireUserCenterAccessAsync()
        //{
        //    PartnerService.Instance.ApiRootUrl = System.Configuration.ConfigurationManager.AppSettings["partnerCenter.apiEndPoint"];
        //    PartnerService.Instance.ApplicationName = "Web Store Front V1.6";
        //    IPartnerCredentials credentials=null;
        //    //IPartnerCredentials credentials = await PartnerCredentials.Instance.GenerateByUserCredentialsAsync(
        //    //   System.Configuration.ConfigurationManager.AppSettings["User.ApplicationId"],

        //    AuthenticationResult aadAuthenticationResult = LoginUserToAad();


        //    AuthenticationResult token = await provider.AccessToken.GetAccessTokenAsync(
        //        $"{provider.Configuration.ActiveDirectoryEndpoint}/{provider.Configuration.PartnerCenterAccountId}",
        //        provider.Configuration.PartnerCenterEndpoint,
        //        new Models.ApplicationCredential
        //        {
        //            ApplicationId = provider.Configuration.ApplicationId,
        //            ApplicationSecret = provider.Configuration.ApplicationSecret,
        //            UseCache = true
        //        },
        //        provider.AccessToken.UserAssertionToken).ConfigureAwait(false);


        //    // Authenticate by user context with the partner service
        //    IPartnerCredentials userCredentials = PartnerCredentials.Instance.GenerateByUserCredentials(
        //        Configuration.UserAuthentication.ApplicationId,
        //        new AuthenticationToken(token.AccessToken, token.ExpiresOn));


        //            //System.Configuration.ConfigurationManager.AppSettings["partnercenter.applicationSecret"],
        //            //System.Configuration.ConfigurationManager.AppSettings["partnercenter.AadTenantId"],
        //            //     System.Configuration.ConfigurationManager.AppSettings["aadEndpoint"],
        //            //     System.Configuration.ConfigurationManager.AppSettings["aadGraphEndpoint"]).ConfigureAwait(false);

        //            return PartnerService.Instance.CreatePartnerOperations(credentials);
        //}


        public static async Task <IPartner> GetUserOperationsAsync(Guid correlationId)
        {
            //AuthenticationResult token = await provider.AccessToken.GetAccessTokenAsync(
            //   $"{System.Configuration.ConfigurationManager.AppSettings["aadEndpoint"].ToString()}/{System.Configuration.ConfigurationManager.AppSettings["partnercenter.AadTenantId"]}",
            //   System.Configuration.ConfigurationManager.AppSettings["partnerCenter.apiEndPoint"],
            //   new Models.ApplicationCredential
            //   {
            //       ApplicationId = System.Configuration.ConfigurationManager.AppSettings["partnercenter.applicationId"],
            //       ApplicationSecret = System.Configuration.ConfigurationManager.AppSettings["partnercenter.applicationSecret"],
            //       UseCache = true
            //   },
            //   provider.AccessToken.UserAssertionToken).ConfigureAwait(false);

            AuthenticationResult token = await provider.AccessToken.GetAccessTokenAsync(
                $"{provider.Configuration.ActiveDirectoryEndpoint}/{provider.Configuration.PartnerCenterAccountId}",
                provider.Configuration.PartnerCenterEndpoint,
                new Models.ApplicationCredential
            {
                ApplicationId     = provider.Configuration.ApplicationId,
                ApplicationSecret = provider.Configuration.ApplicationSecret,
                UseCache          = true
            },
                provider.AccessToken.UserAssertionToken).ConfigureAwait(false);

            IPartnerCredentials credentials = await PartnerCredentials.Instance.GenerateByUserCredentialsAsync(
                provider.Configuration.ApplicationId,
                new AuthenticationToken(token.AccessToken, token.ExpiresOn)).ConfigureAwait(false);

            IAggregatePartner userOperations = PartnerService.Instance.CreatePartnerOperations(credentials);

            return(userOperations.With(RequestContextFactory.Instance.Create(correlationId)));
        }
Beispiel #2
0
        /// <summary>
        /// Gets an aptly configured instance of the partner service.
        /// </summary>
        /// <param name="correlationId">Correlation identifier used when communicating with Partner Center</param>
        /// <returns>An aptly populated instance of the partner service.</returns>
        /// <remarks>
        /// This function will request the necessary access token to communicate with Partner Center and initialize
        /// an instance of the partner service. The application name and correlation identifier are optional values, however,
        /// they have been included here because it is considered best practice. Including the application name makes it where
        /// Microsoft can quickly identify what application is communicating with Partner Center. Specifying the correlation
        /// identifier should be done to easily correlate a series of calls to Partner Center. Both of these properties will
        /// help Microsoft with identifying issues and supporting you.
        /// </remarks>
        private static IPartner GetPartnerService(Guid correlationId)
        {
            IPartnerCredentials credentials = PartnerCredentials.Instance.GenerateByApplicationCredentials(
                ConfigurationManager.AppSettings["PartnerCenter.ApplicationId"],
                ConfigurationManager.AppSettings["PartnerCenter.ApplicationSecret"],
                ConfigurationManager.AppSettings["PartnerCenter.AccountId"]);

            IAggregatePartner partner = PartnerService.Instance.CreatePartnerOperations(credentials);

            PartnerService.Instance.ApplicationName = ApplicationName;

            return(partner.With(RequestContextFactory.Instance.Create(correlationId)));
        }
        /// <summary>
        /// Gets an instance of the partner service that utilizes app only authentication.
        /// </summary>
        /// <param name="correlationId">Correlation identifier for the operation.</param>
        /// <returns>An instance of the partner service.</returns>
        private async Task <IPartner> GetAppOperationsAsync(Guid correlationId)
        {
            if (appOperations == null || appOperations.Credentials.ExpiresAt > DateTime.UtcNow)
            {
                IPartnerCredentials credentials = await GetPartnerCenterAppOnlyCredentialsAsync(
                    $"{service.Configuration.ActiveDirectoryEndpoint}/{service.Configuration.PartnerCenterApplicationTenantId}");

                lock (appLock)
                {
                    appOperations = PartnerService.Instance.CreatePartnerOperations(credentials);
                }

                PartnerService.Instance.ApplicationName = MigrationConstants.ApplicationName;
            }

            return(appOperations.With(RequestContextFactory.Instance.Create(correlationId)));
        }
        /// <summary>
        /// Gets an instance of the partner service that utilizes app only authentication.
        /// </summary>
        /// <param name="correlationId">Correlation identifier for the operation.</param>
        /// <returns>An instance of the partner service.</returns>
        private async Task <IPartner> GetAppOperationsAsync(Guid correlationId)
        {
            if (appOperations == null || appOperations.Credentials.ExpiresAt > DateTime.UtcNow)
            {
                IPartnerCredentials credentials = await GetPartnerCenterCredentialsAsync().ConfigureAwait(false);

                lock (appLock)
                {
                    appOperations = PartnerService.Instance.CreatePartnerOperations(credentials);
                }

                PartnerService.Instance.ApplicationName = ApplicationName;
            }

            // TODO -- Add localization
            // return appOperations.With(RequestContextFactory.Instance.Create(correlationId, service.Localization.Locale));
            return(appOperations.With(RequestContextFactory.Instance.Create(correlationId)));
        }