Skip to content

Access user id from repository/service #2001

Answered by tidyui
maulikshah88 asked this question in Q&A
Discussion options

You must be logged in to vote

The services/repositories does have access to the user context as the framework can be used in any setup, with or with a web context.

The simplest way to achieve this would be to subscribe to the hooks called from the different services in your application, and in that hook get the current user context from your service provider. Then you could use that information within the hook.

Best regards

Edit to include example

The easiest way (although it can be structured better for maintainability) would be to add something like this into your Program.cs before app.Run().

App.Hooks.Pages.RegisterOnBeforeSave(model =>
{
    var httpContextAccessor = app.Services.GetService<IHttpContextAccessor>();

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@tidyui
Comment options

Comment options

You must be logged in to vote
1 reply
@maulikshah88
Comment options

Answer selected by maulikshah88
Comment options

You must be logged in to vote
1 reply
@tidyui
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
2 participants