如何在Graphql.NETCore3.1中实现JWT?

如何在Graphql.NETCore3.1中实现JWT?,graphql,jwt,jwt-auth,Graphql,Jwt,Jwt Auth,如何实现JWT并将令牌和用户数据返回到游戏场 如何验证用户。。 当前代码 public LoginAuth(DataContext context, IConfiguration configuration) { _context = context; _configuration = configuration; Field<LoginType>( "login",

如何实现JWT并将令牌和用户数据返回到游戏场

如何验证用户。。 当前代码

  public LoginAuth(DataContext context, IConfiguration configuration)
    {
        _context = context;
        _configuration = configuration;

        Field<LoginType>(
         "login",
         arguments: new QueryArguments(
            //se debe modificar por dato, no por tabla <--
            new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "email" },
            new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "contrasena" }

            ),
         resolve: context =>
         {
             var email = context.GetArgument<string>("email");
             var contrasena = context.GetArgument<string>("contrasena");
             //var id = context.GetArgument<int>("id");

             //ServiceResponse<string> response = new ServiceResponse<string>();

             var usuariodb = _context.Usuarios.SingleOrDefault(x => x.Email == email);

             if (usuariodb == null)
             {
                 context.Errors.Add(new ExecutionError("usuario no existe"));
                 return null;
             }

              //response.Data = CreateToken(usuariodb);

             return CreateToken(usuariodb);

         });

    }
公共登录(数据上下文上下文、IConfiguration配置)
{
_上下文=上下文;
_配置=配置;
场(
“登录”,
参数:新的QueryArguments(
//请修改数据,无表格
{
var email=context.GetArgument(“电子邮件”);
var contrasena=context.GetArgument(“contrasena”);
//var id=context.GetArgument(“id”);
//ServiceResponse=新的ServiceResponse();
var usuariodb=\u context.Usuarios.SingleOrDefault(x=>x.Email==Email);
if(usuariodb==null)
{
添加(新的ExecutionError(“usuario no existe”);
返回null;
}
//response.Data=CreateToken(usuariodb);
返回CreateToken(通常为DB);
});
}
如果已验证用户..如何生成令牌。。 生成JWT令牌代码:

private string CreateToken(Usuario user)
    {
        List<Claim> claims = new List<Claim>
          {
            new Claim(ClaimTypes.NameIdentifier, user.Id_usuario.ToString()),
            new Claim(ClaimTypes.Name, user.Email)
         };

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

        //With that key, we create new SigningCredentials and use the HmacSha512Signature algorithm for that.
        SigningCredentials creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

        SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor
        {
            Subject = new ClaimsIdentity(claims),
            Expires = DateTime.Now.AddDays(1),
            SigningCredentials = creds
        };

        JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
        SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);

        return tokenHandler.WriteToken(token);
    }
私有字符串CreateToken(Usuario用户)
{
清单索赔=新清单
{
新声明(ClaimTypes.NameIdentifier,user.Id\u usuario.ToString()),
新索赔(ClaimTypes.Name、user.Email)
};
SymmetricSecurityKey=新的SymmetricSecurityKey(Encoding.UTF8
.GetBytes(_configuration.GetSection(“AppSettings:Token”).Value));
//使用该密钥,我们创建新的签名凭证,并为此使用HMACSHA512签名算法。
SigningCredentials creds=新的签名凭证(key,SecurityAlgorithms.HmacSha512Signature);
SecurityTokenDescriptor tokenDescriptor=新的SecurityTokenDescriptor
{
主题=新的索赔实体(索赔),
Expires=DateTime.Now.AddDays(1),
签名凭证=信誉
};
JwtSecurityTokenHandler tokenHandler=新的JwtSecurityTokenHandler();
SecurityToken token=tokenHandler.CreateToken(tokenDescriptor);
返回tokenHandler.WriteToken(令牌);
}
现时成绩:

“错误”:[ { “消息”:“GraphQL.ExecutionError:对于“LoginType”,类型应为“WebAppEF1.GraphQL.Types.LoginType”“但是得到了:eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9。EYJUYU1LAWQOIIYIYWIW5PCXVLX25HBWUIOIYWWWWYWWYW2WILKJUYIYIYZMTKSIMV6CCITYWJG1NJOSCWOSWIYW5FQ。ZYVAPFEDIJL3BWQSHF38S1R9ID3P46TWI8JV3A-KY8KPASD47V8FHZD8FHZD8FHZWJJJJJJ7W执行策略。\r\r\n(ExecutionContext,ExecutionNode节点)\r\n位于GraphQL.Execution.ExecutionStrategy.ExecuteNodeAsync(ExecutionContext,ExecutionNode节点)”,:

注意你的评论!(: