我正在与用户Manager讨论一个问题。 FindByNameAsync in an ASP.NET 即便数据库中有用户,其回归的核心应用也是无效的。 我试图根据用户名称检索用户角色,但这种方法没有找到用户。
public async Task<string> GetUserRolesByUserId(string userName)
{
var user = await _userManager.FindByNameAsync(userName);
if (user == null)
{
return "";
}
var roles = await _userManager.GetRolesAsync(user);
return string.Join(",", roles);
}
Expected Behavior: The method should find the user "TonyMontana" and return their roles.
Actual Behavior: FindByNameAsync returns null for the user "TonyMontana", even though this user exists in the database with the exact same username.
<<>信息>:
I have confirmed that the user "TonyMontana" exists in the database. The username in the database matches the case of the username passed to FindByNameAsync. The UserManager instance is configured in the Startup.cs. I m not sure why FindByNameAsync is unable to find the user. Is this a case sensitivity issue, or could there be something wrong with how the UserManager is configured or used in my application?
任何见解或建议都将受到高度赞赏。