从编码的jwt字符串获取用户名作为属性

时间:2019-03-06 21:44:10

标签: .net-core angular2-jwt

我收到一个令牌字符串,该字符串由.net Jwt算法编码并在客户端 我尝试对该字符串进行解码,并希望获得在此令牌内添加的用户名。

这是后端和客户端。

 [HttpPost("token")]
            public IActionResult Index()
            {
    ...
                var claim = new[] { new Claim(ClaimTypes.Name, "MyUserName")};
                var keySing = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("my_scret_key_from_ConfigFile"));
                var signedKey = new SigningCredentials(keySing, SecurityAlgorithms.HmacSha256Signature);
                var token = new JwtSecurityToken(
                    issuer: "mysite.com",
                    audience: "mysite.com",
                    expires: DateTime.Now.AddMinutes(1),
                    claims: claim,
                    signingCredentials: signedKey);
                var tokenString = new JwtSecurityTokenHandler().WriteToken(token);
                return Ok(tokenString);
            }

解码令牌字符串;

import jwt_decode from 'jwt-decode';
var dd = jwt_decode(token)

输出对象:

aud: "mysite.com"
exp: 1551908094
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: "MyUserName"
iss: "mysite.com"

如此处所示,用户名是查询字符串的一部分,但我希望像其他名称一样获得常规属性:(name:MyUserName) 我该如何实现?

0 个答案:

没有答案