Ok, not sure if this is the correct answer, but I found a way to stop .net core from storing all that information in cookies. In the startup.cs I add the following line:
services.AddScoped<IUserClaimsPrincipalFactory<User>, AppClaimsPrincipalFactory>();
And then I created the AppClaimsPrinzipalFactory.cs which contains the following:
public class AppClaimsPrincipalFactory : UserClaimsPrincipalFactory<User, Role> {
public AppClaimsPrincipalFactory(UserManager<User> userManager, RoleManager<Role> roleManager, IOptions<IdentityOptions> optionsAccessor)
: base(userManager, roleManager, optionsAccessor) {
}
public override async Task<ClaimsPrincipal> CreateAsync(User user) {
if (user == null) {
throw new ArgumentNullException(nameof(user));
}
var userId = await UserManager.GetUserIdAsync(user);
var userName = await UserManager.GetUserNameAsync(user);
var id = new ClaimsIdentity("Identity.Application",
Options.ClaimsIdentity.UserNameClaimType,
Options.ClaimsIdentity.RoleClaimType);
id.AddClaim(new Claim(Options.ClaimsIdentity.UserIdClaimType, userId));
id.AddClaim(new Claim(Options.ClaimsIdentity.UserNameClaimType, userName));
if (UserManager.SupportsUserSecurityStamp) {
id.AddClaim(new Claim(Options.ClaimsIdentity.SecurityStampClaimType,
await UserManager.GetSecurityStampAsync(user)));
}
// code removed that adds the role claims
if (UserManager.SupportsUserClaim) {
var claims = await UserManager.GetClaimsAsync(user);
id.AddClaims(claims);
}
return new ClaimsPrincipal(id);
}
}
Not sure what exactly happens here but it works. Found this answer here: Recommended best practice for role claims as permissions