/// <summary> /// Gets an aptly configured client. /// </summary> /// <param name="account">The account information to be used when generating the client.</param> /// <param name="environment">The environment where the client is connecting.</param> /// <param name="redirectUri">The redirect URI for the client.</param> /// <returns>An aptly configured client.</returns> public IClientApplicationBase GetClient(MgmtAccount account, MgmtEnvironment environment, string redirectUri = null) { IClientApplicationBase app; if (account.IsPropertySet(MgmtAccountPropertyType.CertificateThumbprint) || account.IsPropertySet(MgmtAccountPropertyType.ServicePrincipalSecret)) { app = CreateConfidentialClient( GetAzureCloudInstance(environment), account.GetProperty(MgmtAccountPropertyType.ApplicationId), account.GetProperty(MgmtAccountPropertyType.ServicePrincipalSecret), GetCertificate(account.GetProperty(MgmtAccountPropertyType.CertificateThumbprint)), redirectUri, account.Tenant); } else { app = CreatePublicClient( GetAzureCloudInstance(environment), account.GetProperty(MgmtAccountPropertyType.ApplicationId), redirectUri, account.Tenant); } return(app); }
/// <summary> /// Executes the operations associated with the cmdlet. /// </summary> public override void ExecuteCmdlet() { Scheduler.RunTask(async() => { MgmtAccount account = new MgmtAccount(); MgmtEnvironment environment = MgmtEnvironment.PublicEnvironments[Environment]; if (!string.IsNullOrEmpty(CertificateThumbprint)) { account.SetProperty(MgmtAccountPropertyType.CertificateThumbprint, CertificateThumbprint); } if (!string.IsNullOrEmpty(RefreshToken)) { account.SetProperty(MgmtAccountPropertyType.RefreshToken, RefreshToken); } account.SetProperty(MgmtAccountPropertyType.ApplicationId, string.IsNullOrEmpty(ApplicationId) ? PowerShellApplicationId : ApplicationId); if (ParameterSetName.Equals(AccessTokenParameterSet, StringComparison.InvariantCultureIgnoreCase)) { account.SetProperty(MgmtAccountPropertyType.AccessToken, AccessToken); account.Type = AccountType.AccessToken; } else if (ParameterSetName.Equals(RefreshTokenParameterSet, StringComparison.InvariantCultureIgnoreCase)) { if (Credential != null) { account.ObjectId = Credential.UserName; account.SetProperty(MgmtAccountPropertyType.ApplicationId, Credential.UserName); account.SetProperty(MgmtAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString()); } } else if (ParameterSetName.Equals(ServicePrincipalCertificateParameterSet, StringComparison.InvariantCultureIgnoreCase)) { account.SetProperty(MgmtAccountPropertyType.ApplicationId, ApplicationId); account.Type = AccountType.Certificate; } else if (ParameterSetName.Equals(ServicePrincipalParameterSet, StringComparison.InvariantCultureIgnoreCase)) { account.ObjectId = Credential.UserName; account.Type = AccountType.ServicePrincipal; account.SetProperty(MgmtAccountPropertyType.ApplicationId, Credential.UserName); account.SetProperty(MgmtAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString()); } else { account.Type = AccountType.User; } if (UseDeviceAuthentication.IsPresent) { account.SetProperty("UseDeviceAuth", "true"); } account.SetProperty(MgmtAccountPropertyType.Scope, $"{environment.GraphEndpoint}/.default"); account.Tenant = string.IsNullOrEmpty(Tenant) ? "organizations" : Tenant; await MgmtSession.Instance.AuthenticationFactory.AuthenticateAsync( account, environment, new[] { account.GetProperty(MgmtAccountPropertyType.Scope) }, Message, CancellationToken).ConfigureAwait(false); MgmtSession.Instance.Context = new MgmtContext { Account = account, Environment = environment }; WriteObject(MgmtSession.Instance.Context); }); }