From ff1a7fff6317297dd60ff0df01f338b1468b716f Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 20 Dec 2023 16:00:24 +0800 Subject: [PATCH] Add `RememberMe` claim to current identity principal. --- .../Volo/Abp/Security/Claims/AbpClaimTypes.cs | 7 ++++++- .../AbpDefaultOpenIddictClaimsPrincipalHandler.cs | 6 ++++++ .../OpenIddict/Controllers/AuthorizeController.cs | 13 +++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/AbpClaimTypes.cs b/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/AbpClaimTypes.cs index 63628bd33a..3ce75e36d8 100644 --- a/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/AbpClaimTypes.cs +++ b/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/AbpClaimTypes.cs @@ -87,9 +87,14 @@ public static class AbpClaimTypes /// Default: "impersonator_username". /// public static string ImpersonatorUserName { get; set; } = "impersonator_username"; - + /// /// Default: "picture". /// public static string Picture { get; set; } = "picture"; + + /// + /// Default: "remember_me". + /// + public static string RememberMe { get; set; } = "remember_me"; } diff --git a/modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo/Abp/OpenIddict/Claims/AbpDefaultOpenIddictClaimsPrincipalHandler.cs b/modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo/Abp/OpenIddict/Claims/AbpDefaultOpenIddictClaimsPrincipalHandler.cs index 7a5b3d2835..95f43e5288 100644 --- a/modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo/Abp/OpenIddict/Claims/AbpDefaultOpenIddictClaimsPrincipalHandler.cs +++ b/modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo/Abp/OpenIddict/Claims/AbpDefaultOpenIddictClaimsPrincipalHandler.cs @@ -26,6 +26,12 @@ public class AbpDefaultOpenIddictClaimsPrincipalHandler : IAbpOpenIddictClaimsPr continue; } + if (claim.Type == AbpClaimTypes.RememberMe) + { + claim.SetDestinations(OpenIddictConstants.Destinations.AccessToken); + continue; + } + switch (claim.Type) { case OpenIddictConstants.Claims.PreferredUsername: diff --git a/modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo/Abp/OpenIddict/Controllers/AuthorizeController.cs b/modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo/Abp/OpenIddict/Controllers/AuthorizeController.cs index 140b0418d5..4b2e3a04b6 100644 --- a/modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo/Abp/OpenIddict/Controllers/AuthorizeController.cs +++ b/modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo/Abp/OpenIddict/Controllers/AuthorizeController.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; @@ -12,6 +13,7 @@ using OpenIddict.Abstractions; using OpenIddict.Server.AspNetCore; using Volo.Abp.AspNetCore.Security; using Volo.Abp.OpenIddict.ViewModels.Authorization; +using Volo.Abp.Security.Claims; namespace Volo.Abp.OpenIddict.Controllers; @@ -123,6 +125,11 @@ public class AuthorizeController : AbpOpenIdDictControllerBase case OpenIddictConstants.ConsentTypes.Explicit when authorizations.Any() && !request.HasPrompt(OpenIddictConstants.Prompts.Consent): var principal = await SignInManager.CreateUserPrincipalAsync(user); + if (result.Properties != null && result.Properties.IsPersistent) + { + principal.AddClaim(AbpClaimTypes.RememberMe, true.ToString()); + } + // Note: in this sample, the granted scopes match the requested scope // but you may want to allow the user to uncheck specific scopes. // For that, simply restrict the list of scopes before calling SetScopes. @@ -216,6 +223,12 @@ public class AuthorizeController : AbpOpenIdDictControllerBase var principal = await SignInManager.CreateUserPrincipalAsync(user); + var result = await HttpContext.AuthenticateAsync(IdentityConstants.ApplicationScheme); + if (result.Succeeded && result.Properties != null && result.Properties.IsPersistent) + { + principal.AddClaim(AbpClaimTypes.RememberMe, true.ToString()); + } + // Note: in this sample, the granted scopes match the requested scope // but you may want to allow the user to uncheck specific scopes. // For that, simply restrict the list of scopes before calling SetScopes.