zoukankan      html  css  js  c++  java
  • JWT

    依赖包:

      System.IdentityModel.Tokens.Jwt

    Code:

     public class JwtModel
            {
                public string PublicKey { get; set; }
                public string PrivateKey { get; set; }
            }
    
            private static readonly string _issuer = "issuer";
            private static readonly string _audience = "test";
            private static readonly string _claimKey = "userId";
            private static readonly string _claimValue = "5435";
    
            public static JwtModel GenerateKey()
            {
                var key = CngKey.Create(CngAlgorithm.ECDsaP256, null, new CngKeyCreationParameters
                {
                    ExportPolicy = CngExportPolicies.AllowPlaintextExport,
                });
    
                return new JwtModel
                {
                    PublicKey = Convert.ToBase64String(key.Export(CngKeyBlobFormat.EccPublicBlob)),
                    PrivateKey = Convert.ToBase64String(key.Export(CngKeyBlobFormat.EccPrivateBlob))
                };
            }
    
            public static string GenerateToken(string privateKey)
            {
                var claims = new[]
               {
                    new Claim(_claimKey, _claimValue),
                    new Claim(JwtRegisteredClaimNames.Sub, "3"),
                    new Claim(JwtRegisteredClaimNames.Jti, Convert.ToBase64String(Guid.NewGuid().ToByteArray())),
                };
                var key = CngKey.Import(Convert.FromBase64String(privateKey), CngKeyBlobFormat.EccPrivateBlob);
    
                var cred = new SigningCredentials(
                    new ECDsaSecurityKey(new ECDsaCng(key)),
                    SecurityAlgorithms.EcdsaSha256);
    
                var token = new JwtSecurityToken(
                    issuer: _issuer,
                    audience: _audience,
                    claims: claims,
                    notBefore: DateTime.UtcNow,
                    expires: DateTime.UtcNow.AddYears(15), //用过 20,18 都不行,还没定位为什么。
                    signingCredentials: cred);
                return new JwtSecurityTokenHandler().WriteToken(token);
            }
    
            public static bool VerifyToken(string token, string publicKey)
            {
                var key = CngKey.Import(
                    Convert.FromBase64String(publicKey), CngKeyBlobFormat.EccPublicBlob);
    
                SecurityToken validatedToken;
                var claims = new JwtSecurityTokenHandler().ValidateToken(
                    token,
                    new TokenValidationParameters
                    {
                        IssuerSigningKey = new ECDsaSecurityKey(new ECDsaCng(key)),
                        ValidAudience = _audience,
                        ValidIssuer = _issuer
                    },
                    out validatedToken);
                return claims.HasClaim(_claimKey, _claimValue);
            }
    

      

  • 相关阅读:
    手动档和自动档
    关于目标:骑行里程破万的感想
    JavaScript基础学习-iterable
    第一个mybatisplus
    MAVEN安装配置
    List和ArrayList的区别
    mysql安装
    Nginx的命令
    Windows Server 2008/2012/2016允许多个用户同时远程桌面
    soapui模拟桩-4 将模拟桩打包成war包
  • 原文地址:https://www.cnblogs.com/zhihang/p/13698992.html
Copyright © 2011-2022 走看看