首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有自签名证书的Saml2Configuration.SigningCertificate中的私钥错误

具有自签名证书的Saml2Configuration.SigningCertificate中的私钥错误
EN

Stack Overflow用户
提问于 2021-08-20 04:57:16
回答 1查看 63关注 0票数 1

我想使用自签名证书来测试这个实现。我正在使用MVC实现,并且正在运行The remote server returned an error: (500) Internal Server Error

调试显示,这个错误发生在line 36: ComputeSignature()Saml2SignedXml.ComputeSignature(...)中,它是从微软的SignedMxl继承过来的,我无法进入它。

Saml2Configuration.SigningCertificate显示了HasPrivateKey = true,但PrivateKey属性出现错误:Saml2Configuration.SigningCertificate.PrivateKey抛出了System.NotSupportedException类型的异常。

在使用provide ITFoxtec证书时,我在同一属性中看到了私钥。

我之所以使用自签名证书,是因为MVC证书会导致tfoxtec.identity.saml2.testidpcore_Certificate.pfx中的Incorrect URI format错误。

老实说,我不认为这是一个代码问题,而是一个证书问题,但我不确定在这一点上应该从哪里看。

EN

回答 1

Stack Overflow用户

发布于 2021-08-20 08:41:37

您可能需要使用另一个自签名证书。

可以在.NET核心或.NET 5.0中创建自签名证书,如下所示:

代码语言:javascript
复制
/// <summary>
/// Create self-signed certificate with subject name. .
/// </summary>
/// <param name="subjectName">Certificate subject name, example: "CN=my-certificate, O=some-organisation".</param>
/// <param name="expiry">Certificate expiry, default 365 days.</param>
public static Task<X509Certificate2> CreateSelfSignedCertificateAsync(this string subjectName, TimeSpan? expiry = null)
{
    using (var rsa = RSA.Create(2048))
    {
        var certRequest = new CertificateRequest(subjectName, rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

        certRequest.CertificateExtensions.Add(
            new X509BasicConstraintsExtension(false, false, 0, false));

        certRequest.CertificateExtensions.Add(
            new X509SubjectKeyIdentifierExtension(certRequest.PublicKey, false));

        certRequest.CertificateExtensions.Add(
            new X509KeyUsageExtension(
                X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.DataEncipherment | X509KeyUsageFlags.KeyAgreement,
                false));

        var now = DateTimeOffset.UtcNow;
        return Task.FromResult(certRequest.CreateSelfSigned(now.AddDays(-1), now.Add(expiry ?? TimeSpan.FromDays(365))));
    }
}

该代码来自ITfoxtec.Identity X509Certificate2Extensions.cs,并且该代码例如用于FoxIDs

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68857190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档