Home > Back-end >  Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) with valid c# con
Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) with valid c# con

Time:09-29

var username = User.FindFirst(ClaimTypes.Email)?.Value;
var userid = new Guid(username);

Why is this causing a Guid FormatException to occur?

Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

The constructor for Guid allows me to pass in a string and I passed in a string so it should work!

CodePudding user response:

The error message really says it all - Guid's constructor doesn't take any odd string, it expects a string in a very specific format - "32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)", e.g. "12345678-1234-1234-1234-12345678".

CodePudding user response:

That is because the claim e-mail contains the e-mail and not the object guid. Check the debugger which claim contains the GUID. Generally the NameIdentifies is the claim you are searching for:

User.FindFirst(ClaimTypes.NameIdentifier)
  • Related