- 确认数据库中AspNetUsers表的数据是否正确。可以使用SQL Server Management Studio检查。
- 确认连接字符串是否正确。
- 确认Identity服务是否已经添加到应用程序中。
- 确认Identity服务的配置是否正确。可以在Startup.cs文件中找到。
- 如果Identity服务被正确地配置了,但仍然返回空值,可以尝试使用下面的代码添加默认的管理员角色和用户:
var roleManager = serviceProvider.GetRequiredService>();
var userManager = serviceProvider.GetRequiredService>();
await roleManager.CreateAsync(new IdentityRole("admin"));
var user = new ApplicationUser
{
UserName = "admin@example.com",
Email = "admin@example.com"
};
await userManager.CreateAsync(user, "Password1!");
await userManager.AddToRoleAsync(user, "admin");
- 启用日志记录并查看日志以了解问题的原因。可以在appsettings.json文件中启用日志记录。
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}