In addition to existing answers I'd like to add that you can also have a class instance available app-wide which holds user-related data like UserID
etc.
It may be useful for refactoring e.g. you don't want to fetch UserID
in every controller action and declare an extra UserID
parameter in every method related to Service Layer.
I've done a research and here's my post.
You just extend your class which you derive from DbContext
by adding UserId
property (or implement a custom Session
class which has this property).
At filter level you can fetch your class instance and set UserId
value.
After that wherever you inject your instance - it will have the necessary data (lifetime must be per request, so you register it using AddScoped
method).
Working example:
public class AppInitializationFilter : IAsyncActionFilter
{
private DBContextWithUserAuditing _dbContext;
public AppInitializationFilter(
DBContextWithUserAuditing dbContext
)
{
_dbContext = dbContext;
}
public async Task OnActionExecutionAsync(
ActionExecutingContext context,
ActionExecutionDelegate next
)
{
string userId = null;
int? tenantId = null;
var claimsIdentity = (ClaimsIdentity)context.HttpContext.User.Identity;
var userIdClaim = claimsIdentity.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
if (userIdClaim != null)
{
userId = userIdClaim.Value;
}
var tenantIdClaim = claimsIdentity.Claims.SingleOrDefault(c => c.Type == CustomClaims.TenantId);
if (tenantIdClaim != null)
{
tenantId = !string.IsNullOrEmpty(tenantIdClaim.Value) ? int.Parse(tenantIdClaim.Value) : (int?)null;
}
_dbContext.UserId = userId;
_dbContext.TenantId = tenantId;
var resultContext = await next();
}
}
For more information see my answer.