I'm using the default authorization in my .NET Core project. I want to check if an user is admin so in ApplicationUser.cs model I've added this:
public class ApplicationUser : IdentityUser
{
public bool admin { get; set; }
}
I migrated these changes into my SQL database and I can see the 'admin' property inside the AspNetUsers table. How do I check the 'admin' property of the currently logged in user?
I agree with the above answer by Peter to just use the
user
variable and check foruser.admin
, however, I strongly suggest extending your project to use a simple Role-based Authorization to simplify things in the long run. You can achieve this with the following documentation: https://docs.microsoft.com/en-us/aspnet/core/security/authorization/rolesYou can access the current user instance through the
UserManager<T>
object, which can be injected into your controller as a constructor parameter. The methodGetUserAsync
takes aClaimsPrincipal
, which in this case is the User of theHttpContext
.