我正在使用从第三方API收到的身份验证令牌。我在下面给出了解码令牌的样本,
{
"nbf": 1564128888,
"exp": 1564132488,
"iss": "http://example.com:5002",
"aud": "http://example.com:5002/resources",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress": "Micky@gmail.com",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "Micky Mouse",
"amr": ["custom"]
}
我正在努力阅读javascript中的“名称”声明。如何在javascript或打字稿中读取该属性?
最佳答案
您可以像这样访问复杂的属性名称:
const name = token["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"]
您也可以将其抽象出来以实现可重用性(like the
ClaimTypes
in C#)const ClaimTypes = {
name: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
// other relevant claims
};
const name = token[ClaimTypes.name];