Ejemplo n.º 1
0
        private async Task <HttpClient> GetHttpClientAsync()
        {
            var configuration     = serviceProvider.GetRequiredService <IConfiguration>();
            var tokenRetriever    = serviceProvider.GetRequiredService <ITokenRetriever>();
            var tokenCacheService = serviceProvider.GetRequiredService <ITokenCacheService>();

            var scopes = new string[] {
                $"api://{configuration["ClientId"]}/access_as_user"
            };
            var log          = new ConsoleLogger("Default", null, true);
            var tokenManager = new TokenManager(configuration, tokenCacheService, log);
            var token        = await tokenRetriever.GetTokenByAuthorizationCodeFlowAsync(scopes);

            var uri = await tokenManager.GetAuthUriAsync(scopes);

            var authCode = await tokenRetriever.GetAuthCodeByMsalUriAsync(uri);

            var authResult = await tokenManager.GetAccessTokenFromCodeAsync(authCode, scopes);

            var tokenManagerHttpMessageHandler = new TokenManagerHttpMessageHandler(log, tokenManager, authResult.AccessToken);
            var httpClient = new HttpClient(tokenManagerHttpMessageHandler);

            httpClient.BaseAddress = new Uri(configuration["GraphBaseUri"]);
            var desiredScopes = new string[]
            {
                "Group.ReadWrite.All",
                "User.ReadBasic.All"
            };

            httpClient.DefaultRequestHeaders.Add("X-LOBScopes", desiredScopes);
            return(httpClient);
        }
        public static void BuildServiceProvider(ILogger logger, string accessToken)
        {
            if (serviceProvider != null)
            {
                return;
            }

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(logger);
            serviceCollection.AddSingleton <IConfiguration, ConfigurationSettings>();
            serviceCollection.AddSingleton <ITokenCacheService, TokenCacheService>();
            serviceCollection.AddSingleton <ITokenManager, TokenManager>();

            serviceCollection.AddSingleton <HttpClient, HttpClient>((sp) =>
            {
                var tokenManager = sp.GetRequiredService <ITokenManager>();
                var tokenManagerHttpMessageHandler = new TokenManagerHttpMessageHandler(logger, tokenManager, accessToken);

                var httpClient = new HttpClient(tokenManagerHttpMessageHandler)
                {
                    BaseAddress = new Uri(sp.GetRequiredService <IConfiguration>()["GraphBaseUri"])
                };
                var desiredScopes = new string[]
                {
                    "Group.ReadWrite.All",
                    "User.ReadBasic.All"
                };
                httpClient.DefaultRequestHeaders.Add("X-LOBScopes", desiredScopes);

                return(httpClient);
            });
            serviceCollection.AddSingleton <ITeamsManager, TeamsManager>();
            serviceCollection.AddSingleton <IUserManager, UserManager>();
            serviceCollection.AddSingleton <IOneDriveManager, OneDriveManager>();
            serviceCollection.AddSingleton <IAzureManager, AzureManager>();
            serviceCollection.AddSingleton <IStorageService, StorageService>();

            serviceProvider = serviceCollection.BuildServiceProvider();
        }