c# - How to use the ClaimNames instead of the url? - Stack Overflow

admin2025-04-19  0

This code returns null when I call the function:

var username = user.Claims
                   .FirstOrDefault(x => x.Type == JwtRegisteredClaimNames.GivenName)
                   .Value;
return username;

The code returns the logged in user's name when I use this url instead of JwtRegisteredClaimNames.GivenName:

var username = user.Claims
                   .FirstOrDefault(x => x.Type == ";)
                   .Value;
return username;

Is there any other way to make this code work than using this url ?

This code returns null when I call the function:

var username = user.Claims
                   .FirstOrDefault(x => x.Type == JwtRegisteredClaimNames.GivenName)
                   .Value;
return username;

The code returns the logged in user's name when I use this url instead of JwtRegisteredClaimNames.GivenName:

var username = user.Claims
                   .FirstOrDefault(x => x.Type == "http://schemas.xmlsoap./ws/2005/05/identity/claims/givenname")
                   .Value;
return username;

Is there any other way to make this code work than using this url http://schemas.xmlsoap./ws/2005/05/identity/claims/givenname?

Share Improve this question edited Mar 3 at 19:34 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 3 at 18:29 GandalfGandalf 131 silver badge4 bronze badges 1
  • what's in JwtRegisteredClaimNames.GivenName? – Daniel A. White Commented Mar 3 at 18:33
Add a comment  | 

1 Answer 1

Reset to default 1

The string value of JwtRegisteredClaimNames.GivenName is just "given_name".

You should use System.Security.Claims.ClaimTypes.GivenName which is exactly "http://schemas.xmlsoap./ws/2005/05/identity/claims/givenname".

docs

using System.Security.Claims;

var username = user.Claims.FirstOrDefault(x => x.Type == ClaimTypes.GivenName).Value;
return username;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745077729a283666.html

最新回复(0)