-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PT-13919: SSO is reset to Password after some period. (#2708)
fix: SSO is reset to Password after some period by implementing SecurityStampValidatorOptions.OnRefreshingPrincipal
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/VirtoCommerce.Platform.Security/ConfigureSecurityStampValidatorOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Options; | ||
using VirtoCommerce.Platform.Security.Extensions; | ||
|
||
namespace VirtoCommerce.Platform.Security | ||
{ | ||
public class ConfigureSecurityStampValidatorOptions : IConfigureOptions<SecurityStampValidatorOptions> | ||
{ | ||
public void Configure(SecurityStampValidatorOptions options) | ||
{ | ||
options.ValidationInterval = TimeSpan.FromMinutes(30); | ||
|
||
// When refreshing the principal, ensure custom claims that | ||
// might have been set with an external identity continue | ||
// to flow through to this new one. | ||
options.OnRefreshingPrincipal = refreshingPrincipal => | ||
{ | ||
var newIdentity = refreshingPrincipal.NewPrincipal?.Identities.First(); | ||
var currentIdentity = refreshingPrincipal.CurrentPrincipal?.Identities.First(); | ||
if (currentIdentity is not null) | ||
{ | ||
// Since this is refreshing an existing principal, we want to merge all claims. | ||
newIdentity?.MergeAllClaims(currentIdentity); | ||
} | ||
return Task.CompletedTask; | ||
}; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/VirtoCommerce.Platform.Security/Extensions/MergeClaimsIdentityExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Linq; | ||
using System.Security.Claims; | ||
|
||
namespace VirtoCommerce.Platform.Security.Extensions | ||
{ | ||
public static class MergeClaimsIdentityExtensions | ||
{ | ||
public static void MergeAllClaims(this ClaimsIdentity destination, ClaimsIdentity source) | ||
{ | ||
foreach (var claim in source.Claims | ||
.Where(claim => !destination.HasClaim(claim.Type, claim.Value))) | ||
{ | ||
destination.AddClaim(new Claim(claim.Type, claim.Value)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters