Ejemplo n.º 1
0
        /// <summary>
        /// Checks that a Graph client is available.
        /// </summary>
        /// <returns>The Graph client.</returns>
        public static async Task <ActiveDirectoryClient> EnsureGraphClientCreatedAsync()
        {
            // Active Directory service endpoints
            const string AadServiceResourceId  = "https://graph.windows.net/";
            Uri          AadServiceEndpointUri = new Uri("https://graph.windows.net/");

            try
            {
                AuthenticationContext = new AuthenticationContext(CommonAuthority);

                TokenCacheItem cacheItem = null;

                if (AuthenticationContext.TokenCache.ReadItems().Count() > 0)
                {
                    // Bind the AuthenticationContext to the authority that sourced the token in the cache
                    // this is needed for the cache to work when asking for a token from that authority
                    // (the common endpoint never triggers cache hits)
                    cacheItem             = AuthenticationContext.TokenCache.ReadItems().First();
                    AuthenticationContext = new AuthenticationContext(cacheItem.Authority);
                }
                else
                {
                    // Nothing was found in the cache, so let's acquire a token.
                    var token = await AcquireTokenAsync(AuthenticationContext, AadServiceResourceId);

                    // Check the token
                    if (String.IsNullOrEmpty(token))
                    {
                        // User cancelled sign-in
                        return(null);
                    }
                    else
                    {
                        // If a token was acquired, the TokenCache will contain a TokenCacheItem containing
                        // all the details of the authorization.
                        cacheItem = AuthenticationContext.TokenCache.ReadItems().First();
                    }
                }

                // Store the Id of the logged-in user so that we can retrieve more user info later.
                _loggedInUser = cacheItem.UniqueId;

                // Create our ActiveDirectory client.
                var client = new ActiveDirectoryClient(
                    new Uri(AadServiceEndpointUri, cacheItem.TenantId),
                    async() => await AcquireTokenAsync(AuthenticationContext, AadServiceResourceId));

                return(client);
            }
            // The following is a list of all exceptions you should consider handling in your app.
            // In the case of this sample, the exceptions are handled by returning null upstream.
            catch (DiscoveryFailedException dfe)
            {
                MessageDialogHelper.DisplayException(dfe as Exception);

                // Discovery failed.
                AuthenticationContext.TokenCache.Clear();
                return(null);
            }
            catch (MissingConfigurationValueException mcve)
            {
                MessageDialogHelper.DisplayException(mcve);

                // Connected services not added correctly, or permissions not set correctly.
                AuthenticationContext.TokenCache.Clear();
                return(null);
            }
            catch (AuthenticationFailedException afe)
            {
                MessageDialogHelper.DisplayException(afe);

                // Failed to authenticate the user
                AuthenticationContext.TokenCache.Clear();
                return(null);
            }
            catch (ArgumentException ae)
            {
                MessageDialogHelper.DisplayException(ae as Exception);

                // Argument exception
                AuthenticationContext.TokenCache.Clear();
                return(null);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks that an OutlookServicesClient object is available.
        /// </summary>
        /// <returns>The OutlookServicesClient object. </returns>
        public static async Task <OutlookServicesClient> EnsureOutlookClientCreatedAsync()
        {
            try
            {
                AuthenticationContext = new AuthenticationContext(CommonAuthority);

                if (AuthenticationContext.TokenCache.ReadItems().Count() > 0)
                {
                    // Bind the AuthenticationContext to the authority that sourced the token in the cache
                    // this is needed for the cache to work when asking for a token from that authority
                    // (the common endpoint never triggers cache hits)
                    string cachedAuthority = AuthenticationContext.TokenCache.ReadItems().First().Authority;
                    AuthenticationContext = new AuthenticationContext(cachedAuthority);
                }

                // Create a DiscoveryClient using the discovery endpoint Uri.
                DiscoveryClient discovery = new DiscoveryClient(DiscoveryServiceEndpointUri,
                                                                async() => await AcquireTokenAsync(AuthenticationContext, DiscoveryResourceId));

                // Now get the capability that you are interested in.
                CapabilityDiscoveryResult result = await discovery.DiscoverCapabilityAsync("Mail");

                var client = new OutlookServicesClient(
                    result.ServiceEndpointUri,
                    async() => await AcquireTokenAsync(AuthenticationContext, result.ServiceResourceId));

                return(client);
            }
            // The following is a list of all exceptions you should consider handling in your app.
            // In the case of this sample, the exceptions are handled by returning null upstream.
            catch (DiscoveryFailedException dfe)
            {
                MessageDialogHelper.DisplayException(dfe as Exception);

                // Discovery failed.
                AuthenticationContext.TokenCache.Clear();
                return(null);
            }
            catch (MissingConfigurationValueException mcve)
            {
                MessageDialogHelper.DisplayException(mcve);

                // Connected services not added correctly, or permissions not set correctly.
                AuthenticationContext.TokenCache.Clear();
                return(null);
            }
            catch (AuthenticationFailedException afe)
            {
                MessageDialogHelper.DisplayException(afe);

                // Failed to authenticate the user
                AuthenticationContext.TokenCache.Clear();
                return(null);
            }
            catch (ArgumentException ae)
            {
                MessageDialogHelper.DisplayException(ae as Exception);
                // Argument exception
                AuthenticationContext.TokenCache.Clear();
                return(null);
            }
        }