【发布时间】:2018-02-19 06:34:19
【问题描述】:
我们如何在 c# 中创建 AsymmetricSecurityKey。实际上,我们正在使用 AsymetricSecurityKey 创建签名凭据,这是我们的代码:
// Define const Key this should be private secret key stored in some safe place
string key = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";
// Create Security key using private key above:
// not that latest version of JWT using Microsoft namespace instead of System
var securityKey = new AsymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
// Also note that securityKey length should be >256b
// so you have to make sure that your private key has a proper length
//
var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials
(securityKey, SecurityAlgorithms.HmacSha256Signature);
【问题讨论】:
-
在非对称中我不认为可以提供私钥/公钥,它只能生成。
-
@FaizanRabbani,你能给我提供任何在 C# 中生成 AsymmetricSecurityKey 的示例代码吗?
-
AsymmetricSecurityKey是抽象类,需要创建具体的实现。例如,如果您使用 RSA:var securityKey = new RsaSecurityKey(...)
标签: c#