My IUserClaimsPrincipalFactory implementation is causing StackOverflowException on IdentityServer4

isn't this line recursive, the function is calling itself recursively in an endless loop

var principal = await CreateAsync(user);

CreateUser is the function you are in and you call it again recursively which creates an infinite loop, hence stack overflow


First, change the line

public class CustomUserClaimsPrincipalFactory : IUserClaimsPrincipalFactory<ApplicationUser>

to

public class CustomUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser,IdentityRole>

then, change the line

var principal = await CreateAsync(user);

to

 var principal = await base.CreateAsync(user);