FluentCMS
FluentCMS copied to clipboard
Code review for AuthContext
In the below code is the if condition correct? Isn't the User always not null?
public AuthContext(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
_serviceProvider = _httpContextAccessor.HttpContext?.RequestServices;
var user = httpContextAccessor.HttpContext?.User;
if (user != null)
{
var idClaimValue = user.FindFirstValue(ClaimTypes.Sid);
_userId = idClaimValue == null ? Guid.Empty : Guid.Parse(idClaimValue);
_username = user.FindFirstValue(ClaimTypes.NameIdentifier) ?? string.Empty;
_isAuthenticated = user?.Identity?.IsAuthenticated ?? false;
_isApi = user?.FindFirstValue(ClaimTypes.Actor) == "m2m";
}
}
No it's not null. It's populated by our pipeline from jwt token. It's not related to our users in db!
The question is whether there is any condition that user is null. I think even for an anonymous user, the user object is not null.
It would be null if Authcontext is used inside an unauthorized action. You are correct. It seems like it is redundant!