예제 #1
0
        public GraphService(Activity activity)
        {
            AuthClientApp = PublicClientApplicationBuilder.Create(clientID)
                            .WithRedirectUri($"msal{clientID}://auth")
                            .Build();

            var authProvider = new DelegateAuthenticationProvider(async(request) =>
            {
                IEnumerable <IAccount> accounts = await AuthClientApp.GetAccountsAsync();
                AuthenticationResult authResult;
                try
                {
                    authResult = await AuthClientApp.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
                                 .ExecuteAsync();
                }
                catch (MsalUiRequiredException)
                {
                    authResult = await AuthClientApp.AcquireTokenInteractive(scopes)
                                 .WithParentActivityOrWindow(activity)
                                 .ExecuteAsync();
                }
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
            });

            // Use AndroidClientHandler as opposed to HttpClientHandler.
            var innerHandler = new AndroidClientHandler {
                AllowAutoRedirect = false
            };
            var pipeline = GraphClientFactory.CreatePipeline(GraphClientFactory.CreateDefaultHandlers(authProvider), innerHandler);

            GraphClient = new GraphServiceClient(authProvider, new HttpProvider(pipeline, true, new Serializer()));
        }
예제 #2
0
        private IAuthenticationProvider GetAuthProvider()
        {
            AuthClientApp = PublicClientApplicationBuilder.Create(clientID)
                            .WithRedirectUri($"msal{clientID}://auth")
                            .Build();

            return(new DelegateAuthenticationProvider(async(request) =>
            {
                IEnumerable <IAccount> accounts = await AuthClientApp.GetAccountsAsync();
                AuthenticationResult authResult;
                try
                {
                    authResult = await AuthClientApp.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
                                 .ExecuteAsync();
                }
                catch (MsalUiRequiredException)
                {
                    authResult = await AuthClientApp.AcquireTokenInteractive(scopes)
                                 .ExecuteAsync();
                }
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
            }));
        }