Search code examples
c#asp.net-corejwtopenid.net-8.0

Get email claim from identity after upgrading to .NET 8 not working


I upgrade my project from .NET 7 to .NET 8. I know that there is a breaking change with the claims.

In .NET 7 I used to do:

return auth?.User?.Claims?.SingleOrDefault(x => x.Type == "email")?.Value;

In .NET 8, it looks like I need to do:

return auth?.User?.Claims?.SingleOrDefault(x => x.Type == @"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")?.Value;

Is there a better way to retrieve the email claim?


Solution

  • The System.Security.Claims namespace holds a bunch of constants you can use for this. For example:

    using System.Security.Claims;
    
    ...
    
    return auth?.User?.Claims?
        .SingleOrDefault(x => x.Type == ClaimTypes.Email)?.Value;