首页 > 解决方案 > 为自定义 Azure B2C 邀请策略调用 FromSigningCredentials 时,证书不是 RSA 证书

问题描述

我正在为 Azure B2C 尝试此自定义邀请策略代码:

https://github.com/azure-ad-b2c/samples/tree/master/policies/invite#signup-with-email-invitation

我按照分步指南创建了证书,但是当代码遇到方法时FromSigningCredentials,它会抛出异常Certificate is not an RSA certificate.

这是代码

public static JwksKeyModel FromSigningCredentials(X509SigningCredentials signingCredentials)
{
    X509Certificate2 certificate = signingCredentials.Certificate;

    // JWK cert data must be base64 (not base64url) encoded
    string certData = Convert.ToBase64String(certificate.Export(X509ContentType.Cert));

    // JWK thumbprints must be base64url encoded (no padding or special chars)
    string thumbprint = Base64UrlEncoder.Encode(certificate.GetCertHash());

    // JWK must have the modulus and exponent explicitly defined
    RSACng rsa = certificate.PublicKey.Key as RSACng;

    if (rsa == null)
    {
        throw new Exception("Certificate is not an RSA certificate.");
    }

    .
    .
    .

证书已加载,但在执行以下行之后:

RSACng rsa = certificate.PublicKey.Key as RSACng;

rsa一片空白。

在此处输入图像描述

这发生在本地和 Azure 网站上。

我在这里想念什么?

标签: certificatersaazure-ad-b2csamplex509certificate2

解决方案


经过一番研究,我刚刚发现了以下问题@GitHub:https ://github.com/dotnet/corefx/issues/26682

用户bartonjs 告诉以下内容

任何人都不应该调用 cert.PublicKey.Key;您应该改用 cert.GetRSAPublicKey()。

我将问题中的那一行替换为:

// JWK must have the modulus and exponent explicitly defined
RSACng rsa = certificate.GetRSAPublicKey() as RSACng;

现在我可以走了...


推荐阅读