Skip to content

Commit

Permalink
Jmprieur/related to249 (#271)
Browse files Browse the repository at this point in the history
* Changed the DI pattern for Token Acquisition interfaces. (#258)

* Update src/Microsoft.Identity.Web/ServiceCollectionExtensions.cs

Co-authored-by: jennyf19 <jeferrie@microsoft.com>

Co-authored-by: pmaytak <34331512+pmaytak@users.noreply.github.com>
Co-authored-by: jennyf19 <jeferrie@microsoft.com>
  • Loading branch information
3 people authored Jun 29, 2020
1 parent 8578da9 commit 1593428
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/Microsoft.Identity.Web/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Identity.Web
Expand Down Expand Up @@ -39,6 +40,23 @@ public static IServiceCollection AddTokenAcquisition(
throw new ArgumentNullException(nameof(services));
}

ServiceDescriptor tokenAcquisitionService = services.FirstOrDefault(s => s.ServiceType == typeof(ITokenAcquisition));
ServiceDescriptor tokenAcquisitionInternalService = services.FirstOrDefault(s => s.ServiceType == typeof(ITokenAcquisitionInternal));
if (tokenAcquisitionService != null && tokenAcquisitionInternalService != null)
{
if (isTokenAcquisitionSingleton ^ (tokenAcquisitionService.Lifetime == ServiceLifetime.Singleton))
{
// The service was already added, but not with the right lifetime
services.Remove(tokenAcquisitionService);
services.Remove(tokenAcquisitionInternalService);
}
else
{
// The service is already added with the right lifetime
return services;
}
}

// Token acquisition service
services.AddHttpContextAccessor();
if (isTokenAcquisitionSingleton)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Graph;
Expand All @@ -27,7 +29,7 @@ public static void AddMicrosoftGraph(this IServiceCollection services,
// Graph base URL
string graphBaseUrl = configuration.GetValue<string>(graphBaseUrlKey);


services.AddTokenAcquisition(true);
services.AddSingleton<GraphServiceClient, GraphServiceClient>(serviceProvider =>
{
var tokenAquisitionService = serviceProvider.GetService<ITokenAcquisition>();
Expand Down
4 changes: 1 addition & 3 deletions tests/WebAppCallsMicrosoftGraph/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"Domain": "qualified.domain.name",
"TenantId": "22222222-2222-2222-2222-222222222222",
"ClientId": "11111111-1111-1111-11111111111111111",
"CallbackPath": "/signin-oidc",

"SingletonTokenAcquisition": true
"CallbackPath": "/signin-oidc"
},
"MicrosoftGraphBaseUrl": "https://graph.microsoft.com/beta",

Expand Down

0 comments on commit 1593428

Please sign in to comment.