Exemplo n.º 1
0
        public string Build()
        {
            Verify.IsNotNull(nameof(Certificate), Certificate);
            var securityKey = new X509SecurityKey(Certificate);

            var header = new JwtHeader(new SigningCredentials(securityKey, SecurityAlgorithms.RsaSha512));

            var addClaims = new List <Claim>();

            if (WebKey.IsNotEmpty())
            {
                addClaims.Add(new Claim(JwtStandardClaimNames.WebKeyName, WebKey));
            }
            ;

            var payload = new JwtPayload(Issuer, Audience, Claims.Concat(addClaims), NotBefore, Expires, IssuedAt);

            var jwtToken     = new JwtSecurityToken(header, payload);
            var tokenHandler = new JwtSecurityTokenHandler();

            return(tokenHandler.WriteToken(jwtToken));
        }
Exemplo n.º 2
0
        public string Build()
        {
            SigningCredentials signingCredentials;
            string?            kid = null;

            if (PublicPrivateKey == null)
            {
                Certificate.Verify(nameof(Certificate)).IsNotNull();
                var securityKey = new X509SecurityKey(Certificate);
                signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.RsaSha512);
            }
            else
            {
                kid = PublicPrivateKey.Kid.ToString();
                var privateSecurityKey = new RsaSecurityKey(PublicPrivateKey.GetPrivateKey());

                signingCredentials = new SigningCredentials(privateSecurityKey, SecurityAlgorithms.RsaSha512);
            }

            var header = new JwtHeader(signingCredentials);

            header["kid"] = kid ?? header["kid"];

            var addClaims = new List <Claim>();

            if (!WebKey.IsEmpty())
            {
                addClaims.Add(new Claim(JwtStandardClaimNames.WebKeyName, WebKey));
            }
            ;

            var payload = new JwtPayload(Issuer, Audience, Claims.Concat(addClaims), NotBefore, Expires, IssuedAt);

            var jwtToken     = new JwtSecurityToken(header, payload);
            var tokenHandler = new JwtSecurityTokenHandler();

            return(tokenHandler.WriteToken(jwtToken));
        }
Exemplo n.º 3
0
        public string Build()
        {
            PrincipleSignature.VerifyNotNull($"{nameof(PrincipleSignature)} is required");

            var header = new JwtHeader(PrincipleSignature.GetSigningCredentials());

            if (!PrincipleSignature.Kid.IsEmpty())
            {
                header["kid"] = PrincipleSignature.Kid;
            }

            var addClaims = new[]
            {
                PrincipleSignature.Subject.IsEmpty() ? null : new Claim(JwtStandardClaimNames.SubjectName, PrincipleSignature.Subject),
                Digest.IsEmpty() ? null : new Claim(JwtStandardClaimNames.DigestName, Digest),
            }.Where(x => x != null);

            var payload = new JwtPayload(PrincipleSignature.Issuer, PrincipleSignature.Audience, Claims.Concat(addClaims), NotBefore, Expires, IssuedAt);

            var jwtToken     = new JwtSecurityToken(header, payload);
            var tokenHandler = new JwtSecurityTokenHandler();

            return(tokenHandler.WriteToken(jwtToken));
        }
Exemplo n.º 4
0
 public void AddClaim(string claim)
 {
     Require.ArgumentNotNullEmpty(claim, nameof(claim));
     Claims = Claims.Concat(new[] { claim }).Distinct().ToArray();
 }