【问题标题】:Adding .cer file with public key to .NET Core 2 with Sustainsys使用 Sustainsys 将带有公钥的 .cer 文件添加到 .NET Core 2
【发布时间】:2018-04-23 10:27:00
【问题描述】:

我有一个 .cer 文件,其中包含测试 AD FS 身份验证所需的公钥。 我可以轻松地将它添加到解决方案中存在的 Web.config 中的 MVC 示例(设置签名证书文件名)中,它似乎可以工作,但我找不到任何添加到 AspNetCore2 项目中的方法。

我找到的最接近的是 SigningServiceCertificate 但它只有一个 getter 并且我在另一个线程中读到它应该以某种方式将公钥添加到 SigningKeys 集合中,我只是无法弄清楚如何。

谢谢!

【问题讨论】:

    标签: asp.net-core-2.0 saml-2.0 adfs sustainsys-saml2


    【解决方案1】:

    我不确定您到底在问什么,但是我使用带有证书的 Sustainsys,并且使用以下行:

    var idp = new Sustainsys.Saml2.IdentityProvider(new entityId("https://sso.acme.com"), opt.SPOptions) { ... insert properties ... }

    idp.SigningKeys.AddConfiguredKey(new X509Certificate2("customer-certificate.cer"));

    opt.SPOptions.ServiceCertificates.Add(EncryptionHelper.GetX509Certificate2("INSERT-THUMBPRINT-OF-YOUR-CERTIFICATE"));

    EncryptionHelper.GetX509Certificate2 是我为读取证书而编写的自定义助手。

    这是EncryptionHelper 的代码 - 我在网上众多示例之一的背面制作了它。

    public static X509Certificate2 GetX509Certificate2(string thumbprint)
    {
        X509Certificate2 retVal = null;
    
        var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    
        store.Open(OpenFlags.ReadOnly);
    
        var certCollection = store.Certificates;
    
        var signingCert = certCollection.Find(X509FindType.FindByThumbprint, thumbprint, false);
    
        if (signingCert.Count > 0)
        {
            retVal = signingCert[0];
        }
    
        return retVal;
    }
    

    请注意,如果您在 Azure 中运行它,那么您还需要使用这些说明将指纹添加到应用程序设置 - https://docs.microsoft.com/en-us/azure/app-service/app-service-web-ssl-cert-load

    您可以按照这些说明找到指纹 - https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-retrieve-the-thumbprint-of-a-certificate

    【讨论】:

    • 谢谢!这正是我需要的。我确实配置了 IdP 并添加了 AddConfiguredKey 部分,但我不知道我需要使用指纹读取证书,因为在 MVC 示例中这不是必需的 - 引用 .cer 文件的路径就足够了。你能解释一下我该怎么做吗?我是否需要实际安装证书才能获得该指纹?
    • 所以我刚刚安装了证书,我可以看到十六进制格式的指纹值。我是否需要将其转换为十进制并使用它来获取证书并将其添加到 ServiceCertificates?
    • 我认为没有必要进行转换,我只需要使用字符之间没有空格的值,但现在我收到“提供的证书无效,因为它不包含私钥。 "所以我使用这样的东西:var certCollection = store.Certificates;var signingCert = certCollection.Find(X509FindType.FindByThumbprint, "INSERT-THUMBPRINT-OF-YOUR-CERTIFICATE", false);return signingCert[0]
    • 嗯,设法创建了一个私有证书,安装它并通过指纹将它添加到 ServiceCertificates,虽然我不知道我缺少什么,因为我没有收到登录弹出提示-向上/页面。
    • 我已经用更多信息更新了我的原始回复。如果这没有帮助,请提供有关您遇到的错误以及您的应用程序如何配置的更多详细信息。如果我的回答有帮助,请将其标记为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2016-04-26
    • 1970-01-01
    • 2019-12-20
    • 2014-05-11
    • 2019-04-23
    • 2021-02-04
    相关资源
    最近更新 更多