I’ve recently decided to switch from string to GUID as the unique identifier for the IdentityUser. I have successfully applied the migration to the database, but now the UserManager methods don’t work anymore. Finding a user by email with the DbContext works fine (DataContext.Users.SingleAsync(...)
) but returns null with UserManager(UserManager.FindByIdAsync(...)
)
ApplicationUser:
public class ApplicationUser : IdentityUser<Guid>
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public override Guid Id { get; set; }
}
DbContext:
public class DataContext :
IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid, IdentityUserClaim<Guid>, IdentityUserRole<Guid>, IdentityUserLogin<Guid>, IdentityRoleClaim<Guid>, IdentityUserToken<Guid>>,
IDataContext
{
private readonly IUserContextAccessor _userContextAccessor;
public DataContext(DbContextOptions<DataContext> options, IUserContextAccessor userContextAccessor) : base(options)
{
_userContextAccessor = userContextAccessor;
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
Profile Service (I get the error inside the IsActiveAsync
method)
public class ProfileService : IProfileService
{
private readonly IUserClaimsPrincipalFactory<ApplicationUser> _claimsFactory;
private readonly UserManager<ApplicationUser> _userManager;
public ProfileService(
UserManager<ApplicationUser> userManager,
IUserClaimsPrincipalFactory<ApplicationUser> claimsFactory)
{
_userManager = userManager;
_claimsFactory = claimsFactory;
}
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
...
}
public async Task IsActiveAsync(IsActiveContext context)
{
var sub = context.Subject.GetSubjectId();
var user = await _userManager.FindByIdAsync(sub);
context.IsActive = user != null;
}
}
And finally, I register it like this:
services.AddIdentity<ApplicationUser, IdentityRole<Guid>>(options =>
{
options.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<DataContext>()
.AddDefaultTokenProviders();