예제 #1
0
        /// <summary>
        /// Run the cmdlet.
        /// </summary>
        protected override void ProcessRecord()
        {
            // Auth
            SdkAuthResult authResult;

            if (this.ParameterSetName == ParameterSetAppOnly)
            {
                // App-only auth
                authResult = AuthUtils.AuthWithClientCredentials(this.ClientSecret);
            }
            else if (this.ParameterSetName == ParameterSetCertificateThumbprint)
            {
                authResult = AuthUtils.AuthWithCertificateThumbprint(this.CertificateThumbprint);
            }

            else
            {
                // User auth
#if NETFRAMEWORK
                switch (this.ParameterSetName)
                {
                case ParameterSetPSCredential:
                    System.Net.NetworkCredential networkCreds = this.Credential.GetNetworkCredential();
                    authResult = AuthUtils.AuthWithUserCredentials(networkCreds.UserName, networkCreds.Password);
                    break;

                case ParameterSetCertificate:
                    // TODO: Implement Certificate auth
                    throw new PSNotImplementedException();

                case ParameterSetForceInteractive:
                    authResult = AuthUtils.Auth(Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior.SelectAccount);
                    break;

                case ParameterSetForceNonInteractive:
                    authResult = AuthUtils.Auth(Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior.Never);
                    break;

                case ParameterSetAdminConsent:
                    authResult = AuthUtils.GrantAdminConsent();
                    break;

                default:
                    authResult = AuthUtils.Auth();
                    break;
                }
#else
                authResult = AuthUtils.AuthWithDeviceCode(
                    displayDeviceCodeMessageToUser: (deviceCodeMessage) =>
                {
                    this.WriteWarning(deviceCodeMessage);
                },
                    useAdminConsentFlow: this.AdminConsent
                    );
#endif
            }

            // Decide what to return
            if (!this.Quiet)
            {
                if (this.PassThru)
                {
                    // Return the access token
                    this.WriteObject(authResult.AccessToken);
                }
                else
                {
                    // Return details about the logged in user
                    this.WriteObject(authResult.PSUserDisplayableInformation);
                }
            }
        }