0

我无法在 C# 中生成以下有效负载。如果有人可以指导。 在此处输入图像描述

我正在使用下面的代码生成错误的输出。

var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
        var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);


        var permClaims = new List<Claim>();
        permClaims.Add(new Claim("aud", "jitsi"));
        permClaims.Add(new Claim("iss", "8B23A4BA85DE85D2922703F319496934"));
        permClaims.Add(new Claim("sub", "artaticsxauuio.com"));
        permClaims.Add(new Claim("room", "jd"));

        //Create Security Token object by giving required parameters    
        var token = new JwtSecurityToken(issuer, //Issure    
                        issuer,  //Audience    
                        permClaims,
                        expires: DateTime.Now.AddHours(1),
                        signingCredentials: credentials);
 
        var jwt_token = new JwtSecurityTokenHandler().WriteToken(token);

我得到错误的输出:

在此处输入图像描述

先感谢您。我正在为 JITSI 构建这个。

4

1 回答 1

1

这对我有用。

using System.IdentityModel.Tokens.Jwt;
----------------------------------------------------------------------
string key = @"secret-key";

var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));

var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, "HS256");

var header = new JwtHeader(credentials);

JwtPayload payload = new JwtPayload();

payload.AddClaim(new System.Security.Claims.Claim("context", "{'user': { 'name': 'username', 'email': 'email' }}", JsonClaimValueTypes.Json));
payload.AddClaim(new System.Security.Claims.Claim("iss", @"app-key"));
payload.AddClaim(new System.Security.Claims.Claim("aud", "meet.jitsi.com"));
payload.AddClaim(new System.Security.Claims.Claim("sub", "meet.jitsi.com"));
payload.AddClaim(new System.Security.Claims.Claim("room", "TestRoom"));
                
var secToken = new JwtSecurityToken(header, payload);
var handler = new JwtSecurityTokenHandler();

var tokenString = handler.WriteToken(secToken);
于 2021-05-18T13:10:30.060 回答