public static async Task <ActiveDirectoryClient> GetGraphClientAsync()
        {
            //Check to see if this client has already been created. If so, return it. Otherwise, create a new one.
            if (_graphClient != null)
            {
                Debug.WriteLine("Got a Graph client for Users and Groups.");
                return(_graphClient);
            }
            else
            {
                // Active Directory service endpoints
                const string AadServiceResourceId  = "https://graph.windows.net/";
                Uri          AadServiceEndpointUri = new Uri("https://graph.windows.net/");

                try
                {
                    //First, look for the authority used during the last authentication.
                    //If that value is not populated, use _commonAuthority.
                    string authority = null;
                    if (String.IsNullOrEmpty(AuthenticationHelper.LastAuthority))
                    {
                        authority = AuthenticationHelper.CommonAuthority;
                    }
                    else
                    {
                        authority = AuthenticationHelper.LastAuthority;
                    }

                    // Create an AuthenticationContext using this authority.
                    AuthenticationHelper._authenticationContext = new AuthenticationContext(authority);

                    var token = await AuthenticationHelper.GetTokenHelperAsync(AuthenticationHelper._authenticationContext, AadServiceResourceId);

                    // Check the token
                    if (String.IsNullOrEmpty(token))
                    {
                        // User cancelled sign-in
                        return(null);
                    }
                    else
                    {
                        // Create our ActiveDirectory client.
                        _graphClient = new ActiveDirectoryClient(
                            new Uri(AadServiceEndpointUri, AuthenticationHelper.TenantId),
                            async() => await AuthenticationHelper.GetTokenHelperAsync(AuthenticationHelper._authenticationContext, AadServiceResourceId));

                        Debug.WriteLine("Got a Graph client for Users and Groups.");

                        return(_graphClient);
                    }
                }

                catch (Exception)
                {
                    // Argument exception
                }
                AuthenticationHelper._authenticationContext.TokenCache.Clear();
                return(null);
            }
        }
예제 #2
0
        /// <summary>
        /// Checks that an OutlookServicesClient object is available.
        /// </summary>
        /// <returns>The OutlookServicesClient object. </returns>
        public static async Task <OutlookServicesClient> GetOutlookClientAsync()
        {
            if (_outlookClient != null && !String.IsNullOrEmpty(AuthenticationHelper.LastAuthority))
            {
                Debug.WriteLine("Got an Outlook client for Mail.");
                return(_outlookClient);
            }
            else
            {
                try
                {
                    //First, look for the authority used during the last authentication.
                    //If that value is not populated, use CommonAuthority.
                    string authority = null;

                    if (String.IsNullOrEmpty(AuthenticationHelper.LastAuthority))
                    {
                        authority = AuthenticationHelper.CommonAuthority;
                    }
                    else
                    {
                        authority = AuthenticationHelper.LastAuthority;
                    }

                    // Create an AuthenticationContext using this authority.
                    AuthenticationHelper._authenticationContext = new AuthenticationContext(authority);

                    // Set the value of _authenticationContext.UseCorporateNetwork to true so that you
                    // can use this app inside a corporate intranet. If the value of UseCorporateNetwork
                    // is true, you also need to add the Enterprise Authentication, Private Networks, and
                    // Shared User Certificates capabilities in the Package.appxmanifest file.
                    AuthenticationHelper._authenticationContext.UseCorporateNetwork = true;

                    //See the Discovery Service Sample (https://github.com/OfficeDev/Office365-Discovery-Service-Sample)
                    //for an approach that improves performance by storing the discovery service information in a cache.
                    DiscoveryClient discoveryClient = new DiscoveryClient(
                        async() => await AuthenticationHelper.GetTokenHelperAsync(AuthenticationHelper._authenticationContext, AuthenticationHelper.DiscoveryResourceId));

                    // Get the specified capability ("Calendar").
                    CapabilityDiscoveryResult result =
                        await discoveryClient.DiscoverCapabilityAsync("Mail");

                    var token = await AuthenticationHelper.GetTokenHelperAsync(AuthenticationHelper._authenticationContext, result.ServiceResourceId);

                    // Check the token
                    if (String.IsNullOrEmpty(token))
                    {
                        // User cancelled sign-in
                        return(null);
                    }
                    else
                    {
                        _outlookClient = new OutlookServicesClient(
                            result.ServiceEndpointUri,
                            async() => await AuthenticationHelper.GetTokenHelperAsync(AuthenticationHelper._authenticationContext, result.ServiceResourceId));
                        Debug.WriteLine("Got an Outlook client for Mail.");
                        return(_outlookClient);
                    }
                }
                // The following is a list of 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)
                {
                    Debug.WriteLine(dfe.Message);
                }
                catch (ArgumentException ae)
                {
                    Debug.WriteLine(ae.Message);
                }

                AuthenticationHelper._authenticationContext.TokenCache.Clear();

                return(null);
            }
        }