Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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# 使用.Net的JSON Web令牌处理程序生成带有证书指纹的JWT_C#_.net_Jwt - Fatal编程技术网

C# 使用.Net的JSON Web令牌处理程序生成带有证书指纹的JWT

C# 使用.Net的JSON Web令牌处理程序生成带有证书指纹的JWT,c#,.net,jwt,C#,.net,Jwt,我正在开始一项新任务,我必须处理JWT的指纹。我们在Microsoft.Net框架中使用JSON Web令牌处理程序。已经有一个在测试中使用的实现,它生成JWT而不在头文件中归档x5t。看起来是这样的: var handler = new JwtSecurityTokenHandler(); var securityKey = new InMemorySymmetricSecurityKey(Any.Array<byte>(1024)); var desc =

我正在开始一项新任务,我必须处理JWT的指纹。我们在Microsoft.Net框架中使用JSON Web令牌处理程序。已经有一个在测试中使用的实现,它生成JWT而不在头文件中归档x5t。看起来是这样的:

var handler = new JwtSecurityTokenHandler();
      var securityKey = new InMemorySymmetricSecurityKey(Any.Array<byte>(1024));
      var desc = new SecurityTokenDescriptor
      {
        TokenIssuerName = "MSI",
        Lifetime = new Lifetime(null, DateTime.UtcNow.AddDays(10)),
        SigningCredentials = new SigningCredentials(securityKey, "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", "http://www.w3.org/2001/04/xmlenc#sha256"),
      };

      var identity = new ClaimsIdentity();
      identity.AddClaim(new Claim("scope", "msi_unsapi_presence.watch"));
      identity.AddClaim(new Claim("scope", "msi_unsapi_location.watch"));
      identity.AddClaim(new Claim("scope", "msi_unsapi_groupmgt.read"));
      identity.AddClaim(new Claim("scope", "msi_unsapi_groupmgt.write"));
      var jwtToken = handler.CreateToken(desc);
      return jwtToken;
var handler=newjwtsecuritytokenhandler();
var securityKey=新的InMemorySymetricSecurityKey(任意.Array(1024));
var desc=新的SecurityTokenDescriptor
{
TokenIssuerName=“MSI”,
生存期=新生存期(null,DateTime.UtcNow.AddDays(10)),
SigningCredentials=新的SigningCredentials(securityKey)http://www.w3.org/2001/04/xmldsig-more#hmac-sha256“http://www.w3.org/2001/04/xmlenc#sha256"),
};
var identity=new ClaimsIdentity();
添加声明(新声明(“范围”、“msi_unsapi_presence.watch”);
identity.AddClaim(新声明(“范围”、“msi_unsapi_location.watch”);
添加声明(新声明(“范围”、“msi_unsapi_groupmgt.read”);
添加声明(新声明(“范围”、“msi_unsapi_groupmgt.write”);
var jwtToken=handler.CreateToken(desc);
返回jwtToken;

它产生的令牌:
{“typ”:“JWT”,“alg”:“HS256”}.{“scope”:[“msi_unsapi_presence.watch”,“msi_unsapi_location.watch”,“msi_unsapi_groupmgt.read”,“msi_unsapi_groupmgt.write”]
我尝试将SecurityTokenDescriptor的AttachedReference属性设置为以下
AttachedReference=new X509ThumbKeyIdentifierClause(Any.Array(1024))
在令牌中填充x5t字段(我不关心确切的值,我只需要它存在于令牌中以用于测试)但是生成的令牌仍然没有设置此字段。如何在标头中使用不为空的x5t字段生成令牌,最好修改现有代码?

以下是customJsonWebTokenFormat的实现:

您可以使用payload.add()向其添加任何内容

请求令牌将调用yourJsonWebTokenFormat.protect()方法

您应该在自己的OAuthAuthorizationServerProvider中的AuthenticationTicket中设置在示例中构建的标识

诸如此类:

        public class SampleAuthorizationServerProvider : OAuthAuthorizationServerProvider, IOAuthAuthorizationServerProvider
        {
           public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
                {
        // do AD check or other stuff needed to validate the user here
            var ticket = new AuthenticationTicket(identity, props); // props here is a AuthenticationProperties Dictionnary with other stuff that you want in your JwtToken
    context.Validated(ticket);
        }

        public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
//do some check...
context.Validated();
}
    }
因此,您需要实现两个类:
ISecureDataFormat


我不熟悉您的JwtSecurityTokenHandler类,但我在实现中所做的是创建自己的jsonWebTokenFormat来实现ISecureDataFormat(实现protect/unprotect方法)。诀窍在于,在类中,您需要调用JwtPayload(),它是Dictionary()的超类因此,您可以将x5t字段添加到任何您想要的值,只要它是json格式的。然后,您需要调整JwtSecurityTokenHandler以使用新的JwtFormat,然后继续您的标识和声明。我无法修改JwtSecurityTokenHandler类,因为它是一个库类()我真的找不到我可以用什么方式来配置它以满足我的需要。好的,我很快会向您展示一个实现。1)当我向有效负载添加任何内容时,该值不会添加到jwt头中。2) 我不想从划痕创建jwt,而是使用现有的方法。现在,我只是将CreateToken结果向下转换为JwtSecurityToken,并将值添加到头中,就像添加到常规字典一样,这很难看,不仅难看,而且您获得的令牌现在应该是无效的。CreateToken负责向令牌添加签名。如果你以后修改它,我认为签名不会得到更新。因此,您的签名将不再与有效负载匹配。尽管如此,它并不能解决此字段不在标题中的问题,但您的方法确实如此,可以对正文/有效负载执行哪些操作?标题也一样。添加(“x5t”,“您的值”);将在标题中添加一个带有x5t的字段。这与我前面编写的简单设置值没有区别。签名似乎以同样的方式更新。
     OAuthAuthorizationServerOptions OAuthServerOptions = new 

    OAuthAuthorizationServerOptions()
                {
    // provider configuration, token authentication expiracy, etc...
Provider = new SampleAuthorizationServerProvider()
                    AccessTokenFormat = new JsonWebTokenFormat()
                };
        public class SampleAuthorizationServerProvider : OAuthAuthorizationServerProvider, IOAuthAuthorizationServerProvider
        {
           public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
                {
        // do AD check or other stuff needed to validate the user here
            var ticket = new AuthenticationTicket(identity, props); // props here is a AuthenticationProperties Dictionnary with other stuff that you want in your JwtToken
    context.Validated(ticket);
        }

        public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
//do some check...
context.Validated();
}
    }
OAuthAuthorizationServerProvider, IOAuthAuthorizationServerProvider