Example #1
0
        /// <summary>
        /// Gets the parameters used to perform authentication..
        /// </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="scopes">Scopes requested to access a protected service.</param>
        /// <param name="message">The message to be written to the console.</param>
        /// <returns>The parameters used to perform authentication.</returns>
        private AuthenticationParameters GetAuthenticationParameters(MgmtAccount account, MgmtEnvironment environment, IEnumerable <string> scopes, string message = null)
        {
            if (account.IsPropertySet(MgmtAccountPropertyType.AccessToken))
            {
                return(new AccessTokenParameters(account, environment, scopes));
            }
            else if (account.IsPropertySet("UseAuthCode"))
            {
                return(new InteractiveParameters(account, environment, scopes, message));
            }
            else if (account.IsPropertySet(MgmtAccountPropertyType.RefreshToken))
            {
                return(new RefreshTokenParameters(account, environment, scopes));
            }
            else if (account.Type == AccountType.User)
            {
                if (!string.IsNullOrEmpty(account.ObjectId))
                {
                    return(new SilentParameters(account, environment, scopes));
                }
                else if (account.IsPropertySet("UseDeviceAuth"))
                {
                    return(new DeviceCodeParameters(account, environment, scopes));
                }

                return(new InteractiveParameters(account, environment, scopes, message));
            }
            else if (account.Type == AccountType.Certificate || account.Type == AccountType.ServicePrincipal)
            {
                return(new ServicePrincipalParameters(account, environment, scopes));
            }

            return(null);
        }
        /// <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);
        }