简体   繁体   English

c# - 如何使用来自pfx文件的证书对JWT令牌进行签名/取消签名?

[英]How to sign/unsign JWT token using certificates from a pfx file with c#?

I'm using asp.net core and I want to sent (POST) a JWT token that is signed using a private key from a .pfx.我正在使用asp.net 核心,我想发送(POST)一个使用 .pfx 中的私钥签名的 JWT 令牌。 file.文件。 The receiver side must be able to validate the token using the public key from the .pfx file.在接收端必须能够验证令牌使用从.pfx文件的公钥

How do I do that?我怎么做?

Found the solution:找到了解决办法:

        string certPath = @"xxxx";
        string certPass = "xxxx";
        var collection = new X509Certificate2Collection();
        collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet);

        var certificate = collection[0];

        // create the token signed with privaet key
        // 1. create private security key to create the token
        var rsaPrivateKey = certificate.GetRSAPrivateKey();
        var privateSecurityKey = new RsaSecurityKey(rsaPrivateKey);

        var descriptor = new SecurityTokenDescriptor
        {
            Issuer = "me",
            Audience = "you",
            IssuedAt = DateTime.UtcNow,
            NotBefore = DateTime.UtcNow,
            Expires = DateTime.UtcNow.AddMinutes(5),
            Subject = new ClaimsIdentity(new List<Claim> { new Claim("sub", "scott") }),
            SigningCredentials = new SigningCredentials(privateSecurityKey, SecurityAlgorithms.RsaSha256Signature)
        };

        var handler = new JsonWebTokenHandler();

        // 2. create the token
        string jwt = handler.CreateToken(descriptor);


        // validate token using public key
        var rsaPublicKey = certificate.GetRSAPublicKey();
        var publicSecurityKey = new RsaSecurityKey(rsaPublicKey);

        var result = handler.ValidateToken(jwt,
            new TokenValidationParameters
            {
                ValidIssuer = "me",
                ValidAudience = "you",
                IssuerSigningKey = publicSecurityKey
            });

        Assert.True(result.IsValid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用c#从pfx文件中检索证书? - How to retrieve certificates from a pfx file with c#? 如何在 C# 中使用 PFX 证书签署请求? - How to sign an request using PFX certificate in c#? 使用JKS和/或PFX证书从C#客户端调用Java Web服务 - Invoking Java web service from C# client using JKS and/or PFX certificates 当私钥来自pfx时,C#在MVC中使用JWT发送数据 - C# sending data using JWT in MVC when private key is from a pfx 使用coSign,无法弄清楚如何使用.cer / .pfx文件对c#中的pdf文档进行签名和验证 - Using coSign, can not figure out how to use .cer/.pfx files to sign and verify pdf documents in c# 如何使用.Net Core cookie中间件ticketdataformat将Jwt令牌从Api保存到C#中的cookie - How to save Jwt token from Api to cookie in c# using .Net Core cookie middleware ticketdataformat C#如何验证JWT令牌上的签名? - C# How to verify signature on JWT token? 使用 C# 中的 PFX 文件加密字符串 - Encrypting string using a PFX-File in C# KeyVault-如何从C#应用程序获取存储在keyvault中的pfx文件? - KeyVault - How do I get the pfx file stored in keyvault from my C# app? 如何使用C#Web应用程序使用和重定向JWT令牌URL \\ login \\ login.php?token = jwt.goes.here - How to consume and redirect JWT token URL \login\login.php?token=jwt.goes.here using c# web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM