Skip to content

Commit 29bd59b

Browse files
committed
minor corrections
1 parent 22a0b90 commit 29bd59b

File tree

6 files changed

+50
-10
lines changed

6 files changed

+50
-10
lines changed

2-WebApp-graph-user/2-6-BFF-Proxy/CallGraphBFF/ClientApp/src/components/Home.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class Home extends Component {
4444
</ul>
4545
<p>To help you get started, we have also set up:</p>
4646
<ul>
47-
<li><strong>Client-side navigation</strong>. For example, click <em>Profile</em>, then <em>Back</em> to return here.</li>
47+
<li><strong>Client-side navigation</strong>. For example, click <em>Profile</em> after sign-in, then <em>Back</em> to return here.</li>
4848
<li><strong>Development server integration</strong>. In development mode, the development server from <code>create-react-app</code> runs in the background automatically, so your client-side resources are dynamically built on demand and the page refreshes when you modify any file.</li>
4949
<li><strong>Efficient production builds</strong>. In production mode, development-time features are disabled, and your <code>dotnet publish</code> configuration produces minified, efficiently bundled JavaScript files.</li>
5050
</ul>

2-WebApp-graph-user/2-6-BFF-Proxy/CallGraphBFF/Controllers/AuthController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task<ActionResult> Logout()
2525
await HttpContext.SignOutAsync();
2626

2727
List<string> optionList = new List<string> {
28-
CookieAuthenticationDefaults.AuthenticationScheme,
28+
CookieAuthenticationDefaults.AuthenticationScheme,
2929
OpenIdConnectDefaults.AuthenticationScheme
3030
};
3131

2-WebApp-graph-user/2-6-BFF-Proxy/CallGraphBFF/Controllers/ProfileController.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Microsoft.AspNetCore.Authorization;
33
using Microsoft.AspNetCore.Authentication.Cookies;
4-
using Microsoft.Identity.Web;
54
using Microsoft.Identity.Client;
5+
using Microsoft.Identity.Web;
66
using Microsoft.Graph;
77

88
namespace TodoListBFF.Controllers;
99

10-
[Authorize]
10+
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
11+
[AuthorizeForScopes(Scopes = new string[] { "user.read" })]
1112
[Route("api/[controller]")]
1213
[ApiController]
1314
public class ProfileController : Controller
@@ -31,13 +32,17 @@ public async Task<ActionResult<User>> GetProfile()
3132

3233
return Ok(profile);
3334
}
35+
catch (ServiceException svcex) when (svcex.Message.Contains("Continuous access evaluation"))
36+
{
37+
return Unauthorized("Continuous access evaluation challenge occurred\n" + svcex.Message);
38+
}
3439
catch (MsalUiRequiredException ex)
3540
{
36-
return Unauthorized(ex.Message);
41+
return Unauthorized("MsalUiRequiredException occurred while calling the downstream API\n" + ex.Message);
3742
}
3843
catch (Exception ex)
3944
{
40-
return BadRequest(ex.Message);
45+
return BadRequest("An error occurred while calling the downstream API\n" + ex.Message);
4146
}
4247
}
4348
}

2-WebApp-graph-user/2-6-BFF-Proxy/CallGraphBFF/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.IdentityModel.Tokens.Jwt;
22
using Microsoft.AspNetCore.Authentication.Cookies;
33
using Microsoft.Identity.Web;
4+
using Microsoft.Identity.Web.UI;
45

56
var builder = WebApplication.CreateBuilder(args);
67

@@ -22,6 +23,8 @@
2223
options.Cookie.HttpOnly = true;
2324
options.Cookie.IsEssential = true;
2425

26+
options.Events = new RejectSessionCookieWhenAccountNotInCacheEvents();
27+
2528
//options.Events.OnRedirectToLogin = context =>
2629
//{
2730
// context.Response.StatusCode = 401;
@@ -35,9 +38,8 @@
3538
//};
3639
});
3740

38-
builder.Services.AddControllersWithViews();
39-
40-
builder.Services.AddHttpClient();
41+
builder.Services.AddControllersWithViews()
42+
.AddMicrosoftIdentityUI();
4143

4244
var app = builder.Build();
4345

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Authentication.Cookies;
2+
using Microsoft.Identity.Client;
3+
using Microsoft.Identity.Web;
4+
5+
internal class RejectSessionCookieWhenAccountNotInCacheEvents : CookieAuthenticationEvents
6+
{
7+
public async override Task ValidatePrincipal(CookieValidatePrincipalContext context)
8+
{
9+
try
10+
{
11+
var tokenAcquisition = context.HttpContext.RequestServices.GetRequiredService<ITokenAcquisition>();
12+
string token = await tokenAcquisition.GetAccessTokenForUserAsync(
13+
scopes: new[] { "user.read" },
14+
user: context.Principal);
15+
}
16+
catch (MicrosoftIdentityWebChallengeUserException ex)
17+
when (AccountDoesNotExitInTokenCache(ex))
18+
{
19+
context.RejectPrincipal();
20+
}
21+
}
22+
23+
/// <summary>
24+
/// Is the exception thrown because there is no account in the token cache?
25+
/// </summary>
26+
/// <param name="ex">Exception thrown by <see cref="ITokenAcquisition"/>.GetTokenForXX methods.</param>
27+
/// <returns>A boolean telling if the exception was about not having an account in the cache</returns>
28+
private static bool AccountDoesNotExitInTokenCache(MicrosoftIdentityWebChallengeUserException ex)
29+
{
30+
return ex.InnerException is MsalUiRequiredException
31+
&& ((MsalUiRequiredException)ex.InnerException).ErrorCode == "user_null";
32+
}
33+
}

2-WebApp-graph-user/2-6-BFF-Proxy/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Open the project in your IDE (like Visual Studio or Visual Studio Code) to confi
171171
From your shell or command line, execute the following commands:
172172

173173
```console
174-
cd 2-WebApp-graph-user/2-6-BFF-Proxy/CallGraphBFF
174+
cd 2-WebApp-graph-user/2-6-BFF-Proxy
175175
dotnet run
176176
```
177177

0 commit comments

Comments
 (0)