Skip to content

Commit

Permalink
Update setup
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed Dec 2, 2023
1 parent a95b1e8 commit ed69926
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Identity.Client;
using Microsoft.Identity.Web;

namespace BffMicrosoftEntraID.Server;

public class RejectSessionCookieWhenAccountNotInCacheEvents : CookieAuthenticationEvents
{
private readonly string[] _downstreamScopes;

public RejectSessionCookieWhenAccountNotInCacheEvents(string[] downstreamScopes)
{
_downstreamScopes = downstreamScopes;
}

public async override Task ValidatePrincipal(CookieValidatePrincipalContext context)
{
try
{
var tokenAcquisition = context.HttpContext.RequestServices
.GetRequiredService<ITokenAcquisition>();

string token = await tokenAcquisition.GetAccessTokenForUserAsync(scopes: _downstreamScopes,
user: context.Principal);
}
catch (MicrosoftIdentityWebChallengeUserException ex) when (AccountDoesNotExitInTokenCache(ex))
{
context.RejectPrincipal();
}
}

private static bool AccountDoesNotExitInTokenCache(MicrosoftIdentityWebChallengeUserException ex)
{
return ex.InnerException is MsalUiRequiredException
&& (ex.InnerException as MsalUiRequiredException)!.ErrorCode == "user_null";
}
}
12 changes: 11 additions & 1 deletion IssueVerifiableEmployee/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BffMicrosoftEntraID.Server;
using IssuerVerifiableEmployee.Services.GraphServices;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Identity.Web;
Expand Down Expand Up @@ -28,12 +30,20 @@ public void ConfigureServices(IServiceCollection services)

services.AddDistributedMemoryCache();

var scopes = new string[] { "user.read" };
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi(new string[] { "user.read"})
.EnableTokenAcquisitionToCallDownstreamApi(scopes)
.AddMicrosoftGraph()
.AddDistributedTokenCaches();

// If using downstream APIs and in memory cache, you need to reset the cookie session if the cache is missing
// If you use persistent cache, you do not require this.
// You can also return the 403 with the required scopes, this needs special handling for ajax calls
// The check is only for single scopes
services.Configure<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme,
options => options.Events = new RejectSessionCookieWhenAccountNotInCacheEvents(scopes));

services.AddAuthorization(options =>
{
options.FallbackPolicy = options.DefaultPolicy;
Expand Down

0 comments on commit ed69926

Please sign in to comment.