Reputation: 79
I have problem loading user object using FindByIdAsync (FindByIdEmailAsync, FindByNameAsync also doesn't work in this specific case). The FindByIdAsync returns null.
IdentityController.cs
public class IdentityController : Controller
{
private readonly UserManager<ApplicationUser> userManager;
private readonly IIdentityService identity;
private readonly RoleManager<IdentityRole> roleManager;
public IdentityController(UserManager<ApplicationUser> userManager,
IIdentityService identity,
RoleManager<IdentityRole> roleManager)
{
this.userManager = userManager;
this.identity = identity;
this.roleManager = roleManager;
}
public async Task<IActionResult> Roles(string id)
{
var user = await this.userManager.FindByIdAsync(id);
var u = this.userManager.GetUserId(User);
if (user == null)
{
return NotFound();
}
....
The user is in database: user in database and User id is passed to the controller action: Debuger
Have tried some sort of playground with new standard VS2017 ASP.NET Core MVC Template and added only FindByIdAsync on About and it WORKS.
Thanks
Upvotes: 1
Views: 3357
Reputation: 13148
According to your images, your id
string is wrapped in {
and }
symbols, yet that is not the string that is in the database. I also notice that the value you get back from GetUserId
is not wrapped in braces. Either parse out the braces from id
, or use the value of u
, or pass the right value to Roles
.
Ex:
public async Task<IActionResult> Roles(string id)
{
if (id.Length > 0 && id[0] == '{')
id = id.Substring(1, id.Length - 2);
var user = await this.userManager.FindByIdAsync(id);
var u = this.userManager.GetUserId(User);
if (user == null)
{
return NotFound();
}
....
Upvotes: 1