Software Snippets #2
Is a .NET Identity user logged in?
.NET Core's Identity API is honestly kind of a hassle to wrap your head around, initially. It does authN (local and external), authZ, and everything else vaguely in those two spheres, and it does them in a very "you can configure anything" classic .NET kinda way, which means that of course there are classes with names like fucking SecurityStampRefreshingPrincipalContext lurking around in there.
That said, it's very powerful, but one of the most confusing things about it is that there's no User class. Your users are ClaimsPrincipals, and they have Identities, one for each of the different ways they're authenticated with your system. Because of this, there's no simple "Hey, am I logged in? Just answer 'yes' or 'no'" method to call.
But unless you're working on a very complex system, it's likely that your users are only authenticating via a single method, which means they'll always have a definitive primary Identity, which means this little snippet gives you a straight answer to the question above:
var isLoggedIn = claimsPrincipal.Identity?.IsAuthenticated ?? false;
I recommend throwing it into an extension method so you never have to think about it again.