Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为我的JWT创建签名?_C#_Encryption_Jwt_Box - Fatal编程技术网

C# 如何为我的JWT创建签名?

C# 如何为我的JWT创建签名?,c#,encryption,jwt,box,C#,Encryption,Jwt,Box,根据JWT(特别是使用Box.com api),您需要 创建你的标题和声明,对它们进行base64 url编码,用一个点连接它们 。对于Box.com,它将使用RS256 然后将其发送给提供商(同样,在本例中是Box.com),一切都会很好 我有第一步没问题 第二步对我来说有点麻烦 我想我用我的。。。私钥?编辑:不,私钥是用来解密的 尽管有太多的使用HSA的例子,但我需要使用RSA和System.IdentityModel.Tokens.JWT_stuff过程并没有很好的帮助。如果Box.com

根据JWT(特别是使用Box.com api),您需要

  • 创建你的标题和声明,对它们进行base64 url编码,用一个点连接它们

  • 。对于Box.com,它将使用RS256

  • 然后将其发送给提供商(同样,在本例中是Box.com),一切都会很好

  • 我有第一步没问题

    第二步对我来说有点麻烦

  • 我想我用我的。。。私钥?编辑:不,私钥是用来解密的

  • 尽管有太多的使用HSA的例子,但我需要使用RSA和System.IdentityModel.Tokens.JWT_stuff过程并没有很好的帮助。如果Box.com允许使用HSA256,那么我还可以使用一些其他的软件包和库

  • 我已经看过了,但它并没有起到难以置信的作用

    那么我需要做什么来完成第2步呢?换句话说:如何在C#中使用RSA256进行加密?

    快速查看GitHub上的点,其中有一些代码,您可以查看它们在哪里使用RSA

    甚至还有一个展示如何使用它的地方

    在检查BoxJWTAuth时,我看到了这段代码

    private string ConstructJWTAssertion(string sub, string boxSubType)
    {
        byte[] randomNumber = new byte[64];
        using (var rng = new RNGCryptoServiceProvider())
        {
            rng.GetBytes(randomNumber);
        }
    
        var claims = new List<Claim>{
            new Claim("sub", sub),
            new Claim("box_sub_type", boxSubType),
            new Claim("jti", Convert.ToBase64String(randomNumber)),
        };
    
        var payload = new JwtPayload(this.boxConfig.ClientId, AUTH_URL, claims, null, DateTime.UtcNow.AddSeconds(30));
    
        var header = new JwtHeader(signingCredentials: this.credentials);
        if (this.boxConfig.JWTPublicKeyId != null)
            header.Add("kid", this.boxConfig.JWTPublicKeyId);
    
        var token = new JwtSecurityToken(header, payload);
        var tokenHandler = new JwtSecurityTokenHandler();
        string assertion = tokenHandler.WriteToken(token);
        return assertion;
    }
    
    private string-ConstructJWTAssertion(string-sub,string-boxSubType)
    {
    字节[]随机数=新字节[64];
    使用(var rng=new RNGCryptoServiceProvider())
    {
    rng.GetBytes(随机数);
    }
    var索赔=新列表{
    新索赔(“sub”,sub),
    新索赔(“箱式子类型”,箱式子类型),
    新声明(“jti”,Convert.ToBase64String(randomNumber)),
    };
    var payload=newjwtpayload(this.boxConfig.ClientId,AUTH_URL,claims,null,DateTime.UtcNow.AddSeconds(30));
    var header=新JwtHeader(签名凭证:this.credentials);
    如果(this.boxConfig.JWTPublicKeyId!=null)
    header.Add(“kid”,this.boxConfig.JWTPublicKeyId);
    var-token=新的JwtSecurityToken(头,有效负载);
    var tokenHandler=new JwtSecurityTokenHandler();
    字符串断言=tokenHandler.WriteToken(令牌);
    返回断言;
    }
    

    希望这有帮助。

    的确如此。我很难浏览他们的sdk,因为有几个项目无法加载,因为我不在Windows 8/8.1上。这使得实现它们的对象和跟踪代码变得比需要的困难,因为,嘿,我不能运行它。但看到这一点告诉我,我在正确的轨道上完成了我最终的尝试。这给了我我所缺少的框架。竖起大拇指,伙计!谢谢,没问题。很乐意帮忙。