From fb3f59402959b845935c7fe60494a3d13177c3d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Tue, 23 Jan 2024 14:12:59 +0100 Subject: [PATCH] Simplify the authentication properties filtering logic --- .../Controllers/AuthenticationController.cs | 13 +++---------- .../Controllers/AuthenticationController.cs | 15 ++++----------- .../Controllers/AuthenticationController.cs | 17 +++++------------ .../Controllers/AuthenticationController.cs | 15 ++++----------- .../Controllers/AuthenticationController.cs | 13 +++---------- 5 files changed, 19 insertions(+), 54 deletions(-) diff --git a/samples/Balosar/Balosar.Server/Controllers/AuthenticationController.cs b/samples/Balosar/Balosar.Server/Controllers/AuthenticationController.cs index 2fc0ebd1..a1fb4c44 100644 --- a/samples/Balosar/Balosar.Server/Controllers/AuthenticationController.cs +++ b/samples/Balosar/Balosar.Server/Controllers/AuthenticationController.cs @@ -77,17 +77,10 @@ public async Task LogInCallback() // If needed, the tokens returned by the authorization server can be stored in the authentication cookie. // To make cookies less heavy, tokens that are not used are filtered out before creating the cookie. - properties.StoreTokens(result.Properties.GetTokens().Where(token => token switch - { + properties.StoreTokens(result.Properties.GetTokens().Where(token => token.Name is // Preserve the access and refresh tokens returned in the token response, if available. - { - Name: OpenIddictClientAspNetCoreConstants.Tokens.BackchannelAccessToken or - OpenIddictClientAspNetCoreConstants.Tokens.RefreshToken - } => true, - - // Ignore the other tokens. - _ => false - })); + OpenIddictClientAspNetCoreConstants.Tokens.BackchannelAccessToken or + OpenIddictClientAspNetCoreConstants.Tokens.RefreshToken)); // Ask the default sign-in handler to return a new cookie and redirect the // user agent to the return URL stored in the authentication properties. diff --git a/samples/Dantooine/Dantooine.WebAssembly.Server/Controllers/AuthenticationController.cs b/samples/Dantooine/Dantooine.WebAssembly.Server/Controllers/AuthenticationController.cs index f0a94bf7..a6913e3f 100644 --- a/samples/Dantooine/Dantooine.WebAssembly.Server/Controllers/AuthenticationController.cs +++ b/samples/Dantooine/Dantooine.WebAssembly.Server/Controllers/AuthenticationController.cs @@ -130,18 +130,11 @@ public async Task LogInCallback() // If needed, the tokens returned by the authorization server can be stored in the authentication cookie. // // To make cookies less heavy, tokens that are not used are filtered out before creating the cookie. - properties.StoreTokens(result.Properties.GetTokens().Where(token => token switch - { + properties.StoreTokens(result.Properties.GetTokens().Where(token => token.Name is // Preserve the access, identity and refresh tokens returned in the token response, if available. - { - Name: OpenIddictClientAspNetCoreConstants.Tokens.BackchannelAccessToken or - OpenIddictClientAspNetCoreConstants.Tokens.BackchannelIdentityToken or - OpenIddictClientAspNetCoreConstants.Tokens.RefreshToken - } => true, - - // Ignore the other tokens. - _ => false - })); + OpenIddictClientAspNetCoreConstants.Tokens.BackchannelAccessToken or + OpenIddictClientAspNetCoreConstants.Tokens.BackchannelIdentityToken or + OpenIddictClientAspNetCoreConstants.Tokens.RefreshToken)); // Ask the default sign-in handler to return a new cookie and redirect the // user agent to the return URL stored in the authentication properties. diff --git a/samples/Mortis/Mortis.Client/Controllers/AuthenticationController.cs b/samples/Mortis/Mortis.Client/Controllers/AuthenticationController.cs index 10f8fe2c..e78086c8 100644 --- a/samples/Mortis/Mortis.Client/Controllers/AuthenticationController.cs +++ b/samples/Mortis/Mortis.Client/Controllers/AuthenticationController.cs @@ -128,21 +128,14 @@ or Claims.Private.RegistrationId or Claims.Private.ProviderName // Build the authentication properties based on the properties that were added when the challenge was triggered. var properties = new AuthenticationProperties(result.Properties.Dictionary - .Where(item => item switch - { + .Where(item => item.Key is // Preserve the return URL. - { Key: ".redirect" } => true, + ".redirect" or // If needed, the tokens returned by the authorization server can be stored in the authentication cookie. - { - Key: OpenIddictClientOwinConstants.Tokens.BackchannelAccessToken or - OpenIddictClientOwinConstants.Tokens.BackchannelIdentityToken or - OpenIddictClientOwinConstants.Tokens.RefreshToken - } => true, - - // Don't add the other properties to the external cookie. - _ => false - }) + OpenIddictClientOwinConstants.Tokens.BackchannelAccessToken or + OpenIddictClientOwinConstants.Tokens.BackchannelIdentityToken or + OpenIddictClientOwinConstants.Tokens.RefreshToken) .ToDictionary(pair => pair.Key, pair => pair.Value)); context.Authentication.SignIn(properties, identity); diff --git a/samples/Velusia/Velusia.Client/Controllers/AuthenticationController.cs b/samples/Velusia/Velusia.Client/Controllers/AuthenticationController.cs index 419adedd..9f466f78 100644 --- a/samples/Velusia/Velusia.Client/Controllers/AuthenticationController.cs +++ b/samples/Velusia/Velusia.Client/Controllers/AuthenticationController.cs @@ -130,18 +130,11 @@ public async Task LogInCallback() // If needed, the tokens returned by the authorization server can be stored in the authentication cookie. // // To make cookies less heavy, tokens that are not used are filtered out before creating the cookie. - properties.StoreTokens(result.Properties.GetTokens().Where(token => token switch - { + properties.StoreTokens(result.Properties.GetTokens().Where(token => token.Name is // Preserve the access, identity and refresh tokens returned in the token response, if available. - { - Name: OpenIddictClientAspNetCoreConstants.Tokens.BackchannelAccessToken or - OpenIddictClientAspNetCoreConstants.Tokens.BackchannelIdentityToken or - OpenIddictClientAspNetCoreConstants.Tokens.RefreshToken - } => true, - - // Ignore the other tokens. - _ => false - })); + OpenIddictClientAspNetCoreConstants.Tokens.BackchannelAccessToken or + OpenIddictClientAspNetCoreConstants.Tokens.BackchannelIdentityToken or + OpenIddictClientAspNetCoreConstants.Tokens.RefreshToken)); // Ask the default sign-in handler to return a new cookie and redirect the // user agent to the return URL stored in the authentication properties. diff --git a/samples/Velusia/Velusia.Server/Controllers/AuthenticationController.cs b/samples/Velusia/Velusia.Server/Controllers/AuthenticationController.cs index c1bd482b..e1dd78e5 100644 --- a/samples/Velusia/Velusia.Server/Controllers/AuthenticationController.cs +++ b/samples/Velusia/Velusia.Server/Controllers/AuthenticationController.cs @@ -77,17 +77,10 @@ public async Task LogInCallback() // If needed, the tokens returned by the authorization server can be stored in the authentication cookie. // To make cookies less heavy, tokens that are not used are filtered out before creating the cookie. - properties.StoreTokens(result.Properties.GetTokens().Where(token => token switch - { + properties.StoreTokens(result.Properties.GetTokens().Where(token => token.Name is // Preserve the access and refresh tokens returned in the token response, if available. - { - Name: OpenIddictClientAspNetCoreConstants.Tokens.BackchannelAccessToken or - OpenIddictClientAspNetCoreConstants.Tokens.RefreshToken - } => true, - - // Ignore the other tokens. - _ => false - })); + OpenIddictClientAspNetCoreConstants.Tokens.BackchannelAccessToken or + OpenIddictClientAspNetCoreConstants.Tokens.RefreshToken)); // Ask the default sign-in handler to return a new cookie and redirect the // user agent to the return URL stored in the authentication properties.