最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览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;
发布评论

评论列表(0)

  1. 暂无评论