Andrew Casey
Andrew Casey

Reputation: 313

UserManager.GetUserAsync() returns error: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) C#

In my .Net Core web app, I have used the scaffolding feature and a few online examples, to come up with some account pages and manage pages. I was able to successfully register, and I can see the account info in the AspNetUsers table. Most of the other pages in the example run one command that gets the current user, it's in a helper method.

return _userManager.GetUserAsync(HttpContext.User);

That line returns an Application user, which is what I want. However, I keep getting this error: "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)" I'm assuming it's talking about the ID, and I checked the table and the ID is indeed a Guid, and there is one present. I placed the same line in a try/catch and it's the execution of that line that is causing the error. Any ideas?

Edit: My question is actually more related to the question that LarsTech linked below. I used a slight mod to that answer, which I will answer below.

Upvotes: 0

Views: 4931

Answers (1)

Andrew Casey
Andrew Casey

Reputation: 313

Thanks to LarsTech for sharing a link to a very similar question. That link is: ASP.NET Core Identity - get current user

But I tweaked the answer a little, and it worked for me.

So, instead of using this:

return _userManager.GetUserAsync(HttpContext.User);

I used this instead:

ClaimsPrincipal currentUser = this.User;
ApplicationUser user = await _userManager.FindByNameAsync(currentUser.Identity.Name);

return user;

Upvotes: 0

Related Questions