ASP.NET Core User role enum value to Claim

Solution for ASP.NET Core User role enum value to Claim
is Given Below:

I have angular 12 front-end and ASP.NET Core 5 backend. I’m transferring the role through the JWT token. I want the role to be passed as an integer, so I don’t hardcore the role name. The front-end itself will have an enum which will make it easier for me when there are checks like these horizontal menu settings are available for administrators only if (user.role === Role.Administrator), etc.

The problem is that there is no such overload as new("role", user.Role). If I do user.Role.ToString(), it sends Administrator, which is the key and not the value 1.

public class User
{
    public string Id { get; set; }
    public string Email { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public Role Role { get; set; }
}

public enum Role
{
    Administrator = 1,
    DepartmentAdministrator = 2,
    User = 3
}

public Task GetProfileDataAsync(ProfileDataRequestContext context)
{
    var sub = context.Subject.GetSubjectId();
    var user = _userRepository.FindById(sub);

    var claims = new List<Claim>
    {
        new("username", user.Username),
        new("email", user.Email),
        new("role", user.Role) // cannot be int
    };

    context.IssuedClaims = claims;

    return Task.CompletedTask;
}

You can cast the enum:

int role = (int) user.Role;

var claims = new List<Claim>
    {
        new("username", user.Username),
        new("email", user.Email),
        new("role", role.ToString())
    };