Skip to content

Commit 7740365

Browse files
author
Tiago Brenck
committed
Merge branch 'master' into tibre/fix158
# Conflicts: # 4-WebApp-your-API/Client/Controllers/TodoListController.cs # Microsoft.Identity.Web/Client/TokenCacheProviders/Sql/MSALAppSqlTokenCacheProviderExtension.cs
2 parents 7a4a605 + 09a4358 commit 7740365

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

4-WebApp-your-API/Client/Controllers/TodoListController.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Identity.Client;
34
using Microsoft.Identity.Web.Client;
4-
using System.Collections.Generic;
55
using System.Threading.Tasks;
66
using TodoListClient.Services;
77
using TodoListService.Models;
@@ -11,15 +11,14 @@ namespace TodoListClient.Controllers
1111
public class TodoListController : Controller
1212
{
1313
private ITodoListService _todoListService;
14-
private IList<Todo> Model = new List<Todo>();
1514

1615
public TodoListController(ITodoListService todoListService)
1716
{
1817
_todoListService = todoListService;
1918
}
2019

2120
// GET: TodoList
22-
//[MsalUiRequiredExceptionFilter(ScopeKeySection = "TodoList:TodoListScope")]
21+
[MsalUiRequiredExceptionFilter(ScopeKeySection = "TodoList:TodoListScope")]
2322
public async Task<ActionResult> Index()
2423
{
2524
return View(await _todoListService.GetAsync());

Microsoft.Identity.Web/Client/MsalUiRequiredExceptionFilterAttribute.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
using Microsoft.AspNetCore.Http;
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.AspNetCore.Mvc.Filters;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
57
using Microsoft.Identity.Client;
68
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
9+
using System;
710
using System.Collections.Generic;
811
using System.Linq;
912

@@ -25,6 +28,11 @@ public class MsalUiRequiredExceptionFilterAttribute : ExceptionFilterAttribute
2528
{
2629
public string[] Scopes { get; set; }
2730

31+
/// <summary>
32+
/// Key section on the configuration file that holds the scope value
33+
/// </summary>
34+
public string ScopeKeySection { get; set; }
35+
2836
public override void OnException(ExceptionContext context)
2937
{
3038
MsalUiRequiredException msalUiRequiredException = context.Exception as MsalUiRequiredException;
@@ -37,8 +45,27 @@ public override void OnException(ExceptionContext context)
3745
{
3846
if (CanBeSolvedByReSignInUser(msalUiRequiredException))
3947
{
40-
var properties =
41-
BuildAuthenticationPropertiesForIncrementalConsent(Scopes, msalUiRequiredException, context.HttpContext);
48+
// the users cannot provide both scopes and ScopeKeySection at the same time
49+
if (!string.IsNullOrWhiteSpace(ScopeKeySection) && Scopes != null && Scopes.Length > 0)
50+
{
51+
throw new InvalidOperationException($"Either provide the '{nameof(ScopeKeySection)}' or the '{nameof(Scopes)}' to the 'MsalUiRequiredExceptionFilterAttribute'.");
52+
}
53+
54+
// If the user wishes us to pick the Scopes from a particular config setting.
55+
if (!string.IsNullOrWhiteSpace(ScopeKeySection))
56+
{
57+
// Load the injected IConfiguration
58+
IConfiguration configuration = context.HttpContext.RequestServices.GetRequiredService<IConfiguration>();
59+
60+
if (configuration == null)
61+
{
62+
throw new InvalidOperationException($"The {nameof(ScopeKeySection)} is provided but the IConfiguration instance is not present in the services collection");
63+
}
64+
65+
Scopes = new string[] { configuration.GetValue<string>(ScopeKeySection) };
66+
}
67+
68+
var properties = BuildAuthenticationPropertiesForIncrementalConsent(Scopes, msalUiRequiredException, context.HttpContext);
4269
context.Result = new ChallengeResult(properties);
4370
}
4471
}

Microsoft.Identity.Web/Client/TokenCacheProviders/Sql/MSALAppSqlTokenCacheProviderExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ public static IServiceCollection AddSqlPerUserTokenCache(this IServiceCollection
114114
return services;
115115
}
116116
}
117-
}
117+
}

Microsoft.Identity.Web/Microsoft.Identity.Web.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
<PackageReference Include="Microsoft.AspNetCore.App" />
99
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.2.0" />
1010
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureADB2C.UI" Version="2.2.0" />
11-
<PackageReference Include="Microsoft.Identity.Client" Version="4.2.1" />
11+
<PackageReference Include="Microsoft.Identity.Client" Version="4.3.1" />
1212
</ItemGroup>
1313
</Project>

Microsoft.Identity.Web/WebApiStartupHelpers.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public static IServiceCollection AddProtectWebApiWithMicrosoftIdentityPlatformV2
8383
// be used from the controllers.
8484
options.Events = new JwtBearerEvents();
8585

86-
#pragma warning disable 1998
8786
options.Events.OnTokenValidated = async context =>
8887
{
8988
// This check is required to ensure that the Web API only accepts tokens from tenants where it has been consented and provisioned.
@@ -93,9 +92,8 @@ public static IServiceCollection AddProtectWebApiWithMicrosoftIdentityPlatformV2
9392
throw new UnauthorizedAccessException("Neither scope or roles claim was found in the bearer token.");
9493
}
9594

96-
return;
95+
await Task.FromResult(0);
9796
};
98-
#pragma warning restore 1998
9997

10098
// If you want to debug, or just understand the JwtBearer events, uncomment the following line of code
10199
// options.Events = JwtBearerMiddlewareDiagnostics.Subscribe(options.Events);

0 commit comments

Comments
 (0)