Как удалить "http://schemas.xmlsoap.org/ws/2005/05/identity/ URL-адреса из токена в .netcore - PullRequest
0 голосов
/ 27 октября 2018

Это мой код для генерации токена:

            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, appUser.Id.ToString()),
                new Claim(ClaimTypes.Name, appUser.UserName)
            };

            var roles = await _userManager.GetRolesAsync(user);

            foreach (var role in roles)
            {
                claims.Add(new Claim(ClaimTypes.Role, role));
            }


            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));

            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            string issuer = _config["Token:Issuer"];

            var jwtSecurityToken = new JwtSecurityToken(
                issuer: issuer,
                audience:issuer,
                claims: claims,
                expires: DateTime.Now.AddMinutes(5),
                signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
            );

            jwtSecurityToken.Header.Add("kid", requestAPIKey);
            var token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);

Токен выводит это:

{
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier": "7",
  "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name": "ligeros",
  "exp": 1540619440,
  "iss": "http://localhost:5000",
  "aud": "http://localhost:5000"
}

Как сделать так, чтобы вместо этого выводился этот вывод?

{
  "nameid": "7",
  "unique_name": "ligeros",
  "nbf": 1540613190,
  "exp": 1540703190,
  "iat": 1540613190
}

Мне нет дела до nbf, exp и iat.Я просто хочу, чтобы URL был удален и стал nameid, unique_name вместо http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier и http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name.Спасибо!

1 Ответ

0 голосов
/ 27 октября 2018

Я это исправил:

            var claims = new List<Claim>
            {
                new Claim("nameid", appUser.Id.ToString()),
                new Claim("unique_name", appUser.UserName)
            };

            var roles = await _userManager.GetRolesAsync(user);

            foreach (var role in roles)
            {
                claims.Add(new Claim("roles", role));
            }
...