Exemplo n.º 1
0
        /// <summary>
        /// Performs the execution of the command.
        /// </summary>
        protected override void ProcessRecord()
        {
            AuthenticationResult authResult;
            AzureAccount         account = new AzureAccount();

#if NETSTANDARD
            DeviceCodeResult deviceCodeResult;
#else
            AuthorizationResult authorizationResult;
#endif
            IPartnerServiceClient client;
            PartnerEnvironment    environment;
            Uri    authority;
            string clientId;
            string resource;

            if (ParameterSetName.Equals(ServicePrincipalParameterSet, StringComparison.InvariantCultureIgnoreCase))
            {
                account.Properties[AzureAccountPropertyType.ServicePrincipalSecret] = Credential.Password.ConvertToString();
                account.Type = AccountType.ServicePrincipal;
                clientId     = string.IsNullOrEmpty(ApplicationId) ? Credential.UserName : ApplicationId;
            }
            else
            {
                account.Type = AccountType.User;
                clientId     = ApplicationId;
            }

            account.Properties[AzureAccountPropertyType.Tenant] = string.IsNullOrEmpty(TenantId) ? AuthenticationConstants.CommonEndpoint : TenantId;
            environment = PartnerEnvironment.PublicEnvironments[Environment];

            client    = new PartnerServiceClient(httpClient);
            authority = new Uri($"{environment.ActiveDirectoryAuthority}{account.Properties[AzureAccountPropertyType.Tenant]}");

            resource = string.IsNullOrEmpty(Resource) ? environment.PartnerCenterEndpoint : Resource;

            if (!string.IsNullOrEmpty(RefreshToken))
            {
                authResult = client.RefreshAccessTokenAsync(
                    authority,
                    resource,
                    RefreshToken,
                    clientId,
                    Credential?.Password.ConvertToString()).GetAwaiter().GetResult();
            }
            else if (account.Type == AccountType.ServicePrincipal && !Consent.ToBool())
            {
                authResult = client.AcquireTokenAsync(
                    authority,
                    resource,
                    clientId,
                    Credential.Password.ConvertToString()).GetAwaiter().GetResult();
            }
#if NETSTANDARD
            else
            {
                deviceCodeResult = client.AcquireDeviceCodeAsync(
                    authority,
                    resource,
                    clientId,
                    Credential?.Password.ConvertToString()).GetAwaiter().GetResult();

                WriteWarning(deviceCodeResult.Message);

                authResult = client.AcquireTokenByDeviceCodeAsync(
                    authority,
                    deviceCodeResult,
                    Credential?.Password.ConvertToString()).GetAwaiter().GetResult();
            }