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

This code returns null when I call the function:var username = user.Claims.FirstOrDefault(x => x.Ty

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745077727a4609933.html

相关推荐

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

    This code returns null when I call the function:var username = user.Claims.FirstOrDefault(x => x.Ty

    16小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信