Exemple #1
0
        /// <summary>
        /// Acquires a <see cref="Token"/> from the authority via an interactive user logon prompt.
        /// <para/>
        /// Returns a `<see cref="Token"/>` is successful; otherwise <see langword="null"/>.
        /// </summary>
        /// <param name="targetUri">Uniform resource indicator of the resource access tokens are being requested for.</param>
        /// <param name="clientId">Identifier of the client requesting the token.</param>
        /// <param name="resource">Identifier of the target resource that is the recipient of the requested token.</param>
        /// <param name="redirectUri">Address to return to upon receiving a response from the authority.</param>
        /// <param name="queryParameters">optional value, appended as-is to the query string in the HTTP authentication request to the authority.</param>
        public async Task <Token> InteractiveAcquireToken(TargetUri targetUri, string clientId, string resource, Uri redirectUri, string queryParameters = null)
        {
            if (targetUri is null)
            {
                throw new ArgumentNullException(nameof(targetUri));
            }
            if (string.IsNullOrWhiteSpace(clientId))
            {
                throw new ArgumentNullException(nameof(clientId));
            }
            if (string.IsNullOrWhiteSpace(resource))
            {
                throw new ArgumentNullException(nameof(resource));
            }
            if (redirectUri is null)
            {
                throw new ArgumentNullException(nameof(redirectUri));
            }
            if (!redirectUri.IsAbsoluteUri)
            {
                throw new ArgumentException(nameof(redirectUri));
            }

            Token token = null;

            queryParameters = queryParameters ?? string.Empty;

            try
            {
                var authResult = await Adal.AcquireTokenAsync(AuthorityHostUrl,
                                                              resource,
                                                              clientId,
                                                              redirectUri,
                                                              queryParameters);

                if (Guid.TryParse(authResult.TenantId, out Guid tenantId))
                {
                    token = new Token(authResult.AccessToken, tenantId, TokenType.AzureAccess);
                }

                Trace.WriteLine($"authority host URL = '{AuthorityHostUrl}', token acquisition for tenant [{tenantId.ToString("N")}] succeeded.");
            }
            catch (AuthenticationException)
            {
                Trace.WriteLine($"authority host URL = '{AuthorityHostUrl}', token acquisition failed.");
            }

            return(token);
        }
Exemple #2
0
        protected Base(RuntimeContext context)
            : base(context)
        {
            // If the Adal service is already registered, then we do not need to allocate
            // and add a new one.
            var adal = GetService <IAdal>();

            if (adal is null)
            {
                // Since there's no pre-existing Adal service registered with the current
                // context, we'll need to allocate and add one to it.
                adal = new Adal(Context);

                SetService(adal);
            }
        }
Exemple #3
0
        /// <summary>
        /// Acquires a `<see cref="Token"/>` from the authority via an non-interactive user logon.
        /// <para/>
        /// Returns the acquired `<see cref="Token"/>` if successful; otherwise `<see langword="null"/>`.
        /// </summary>
        /// <param name="targetUri">Uniform resource indicator of the resource access tokens are being requested for.</param>
        /// <param name="clientId">Identifier of the client requesting the token.</param>
        /// <param name="resource">Identifier of the target resource that is the recipient of the requested token.</param>
        /// <param name="redirectUri">Address to return to upon receiving a response from the authority.</param>
        public async Task <Token> NoninteractiveAcquireToken(TargetUri targetUri, string clientId, string resource, Uri redirectUri)
        {
            if (targetUri is null)
            {
                throw new ArgumentNullException(nameof(targetUri));
            }
            if (string.IsNullOrWhiteSpace(clientId))
            {
                throw new ArgumentNullException(nameof(clientId));
            }
            if (string.IsNullOrWhiteSpace(resource))
            {
                throw new ArgumentNullException(nameof(resource));
            }
            if (redirectUri is null)
            {
                throw new ArgumentNullException(nameof(redirectUri));
            }
            if (!redirectUri.IsAbsoluteUri)
            {
                var inner = new UriFormatException("Uri is not absolute when an absolute Uri is required.");
                throw new ArgumentException(inner.Message, nameof(redirectUri), inner);
            }

            Token token = null;

            try
            {
                var authResult = await Adal.AcquireTokenAsync(AuthorityHostUrl,
                                                              resource,
                                                              clientId);

                if (Guid.TryParse(authResult.TenantId, out Guid tentantId))
                {
                    token = new Token(authResult.AccessToken, tentantId, TokenType.AzureAccess);

                    Trace.WriteLine($"token acquisition for authority host URL = '{AuthorityHostUrl}' succeeded.");
                }
            }
            catch (AuthenticationException)
            {
                Trace.WriteLine($"token acquisition for authority host URL = '{AuthorityHostUrl}' failed.");
            }

            return(token);
        }