How can AddCustomAction receive data from an outer scope, or service? #481
Unanswered
VictorioBerra
asked this question in
Q&A
Replies: 1 comment
-
What I ended up doing was using a scoped service with a property I can set, and later access. // Startup.cs
services.AddScoped<ICustomRequestContainer, CustomRequestContainer>();
// OpenIdConnectExtensions.cs
// ... somewhere in OnTokenValidated
var customRequestontainer = context.HttpContext.RequestServices.GetRequiredService<ICustomRequestContainer>();
customRequestontainer.NameIdentifierClaim = nameIdentitfierClaimValue;
// ...
dbContext.SaveChanges();
// AuditExtensions.cs
Audit.Core.Configuration.AddCustomAction(ActionType.OnScopeCreated, scope =>
{
var httpContext = contextAccessor.HttpContext;
var customRequestContainer = httpContext.RequestServices.GetRequiredService<ICustomRequestContainer>();
var nameIdentifierFromRequest = customRequestContainer.NameIdentifierClaim;
if (nameIdentifierFromRequest is not null)
{
scope.Event.Environment.CustomFields["NameIdentifierClaim"] = nameIdentifierFromRequest;
}
else
{
scope.Event.Environment.CustomFields["NameIdentifierClaim"] =
httpContext.User.Claims.Single(x => x.Type == ClaimTypes.NameIdentifier).Value;
}
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have the following code:
This has been working great, however, I recently needed to override an OpenIdConnect method:
The issue here is,
HttpContext.User.Claims
is empty in theAudit.Core.Configuration.AddCustomAction
, which is envoked when callingdbContext.SaveChanges()
in theOnTokenValidated
event forAddOpenIdConnect()
.Is there some way to propagate that value, so I can look for it, and receive it in the AddCustomAction scope? Like an outer scope, that it could detect?
Beta Was this translation helpful? Give feedback.
All reactions