0

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.

Andrew Casey
  • 91
  • 1
  • 14
  • 1
    Maybe related: [ASP.NET Core Identity - get current user](https://stackoverflow.com/q/38751616/719186) – LarsTech Apr 19 '23 at 16:50
  • Does this answer your question? [Guid should contain 32 digits with 4 dashes](https://stackoverflow.com/questions/11224493/guid-should-contain-32-digits-with-4-dashes) – Magnetron Apr 19 '23 at 16:52
  • Set a break point and look at the property values on HttpContext.User when it breaks on that line. – rory.ap Apr 19 '23 at 16:52
  • You probably need to pass `HttpContext.User.ID` instead of `HttpContext.User` – Magnetron Apr 19 '23 at 16:53
  • LarsTech - Thank you so much for the link, it was the same problem, but I tweaked the answer a bit for mine. Magnetron - That question was more about a confirmation link, but the person in that question wasn't using the same command as me. – Andrew Casey Apr 19 '23 at 17:38

1 Answers1

0

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;
Andrew Casey
  • 91
  • 1
  • 14