Пример #1
0
        private string CreateToken(Certificate ca)
        {
            var identity = _identityProvider.GetPeerIdentity();

            var claims = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, "QMTOKEN"),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim("Version", ca.TBSCertificate.Version),
                new Claim("SerialNumber", ca.TBSCertificate.SerialNumber),
                new Claim("Signature", ca.TBSCertificate.Signature),
                new Claim("Issuer", ca.TBSCertificate.Issuer),
                new Claim("NotBefore", ca.TBSCertificate.NotBefore.ToString()),
                new Claim("NotAfter", ca.TBSCertificate.NotAfter.ToString()),
                new Claim("Subject", ca.TBSCertificate.Subject),
                new Claim("CAType", ca.TBSCertificate.CAType.ToString()),
                new Claim("PublicKey", ca.TBSCertificate.PublicKey)
            };

            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_identityProvider.GetPrivateKey()));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var token = new JwtSecurityToken(
                issuer: identity.Name,                //签发者
                audience: ca.TBSCertificate.Subject,  //接受者
                claims: claims,                       //自定义属性
                notBefore: DateTime.Now,              //xxx日期之前不被接受
                expires: DateTime.Now.AddMinutes(30), //token的生命周期
                signingCredentials: creds);

            //"iss" #非必须。issuer 请求实体,可以是发起请求的用户的信息,也可是jwt的签发者。
            //"iat" #非必须。issued at。 token创建时间,unix时间戳格式
            //"exp" #非必须。expire 指定token的生命周期。unix时间戳格式
            //"aud" #非必须。接收该JWT的一方。
            //"sub" #非必须。该JWT所面向的用户
            //"nbf" #非必须。not before。如果当前时间在nbf里的时间之前,则Token不被接受;一般都会留一些余地,比如几分钟。
            //"jti" #非必须。JWT ID。针对当前token的唯一标识
            return(new JwtSecurityTokenHandler().WriteToken(token));
        }
Пример #2
0
 public string GetPrivateKey()
 {
     return(_identityProvider.GetPrivateKey());
 }