无法从文件加载 X.509 证书

The X.509 certificate could not be loaded from the file

我正在为 ASP.net MVC 使用 API SAML2.0,我使用 openssl 创建私有和 public 密钥文件,并为私有文件使用了密码。它生成了两个文件 ca.key 和 cas.pem,我使用 ca.key 文件作为私钥但是我收到这个错误

Additional information: The X.509 certificate could not be loaded from the file D:\Test Web Projects\TestSaml\TestSaml\Certificates\ca.key.

我的用户登录到我的 mvc 应用程序登录过程与 SAML 无关。我只是根据我的数据库检查用户。我使用 SAML2.0 的原因是因为我需要将我的用户定向到另一个外部页面,即我的服务提供商。因此,一旦他们点击我页面上的按钮,他们就会被重定向到另一个网站。以下是我构建的示例代码,用于验证其是否有效。

Web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<appSettings>
  <add key="TargetURL" value="https://btat2.paybill.com/consumer/SSO/SSOLogin?clientId=ReadyCapital"/>
  <add key="webpages:Version" value="3.0.0.0" />
  <add key="webpages:Enabled" value="false" />
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />
</system.web>
</configuration>

Saml.config

<?xml version="1.0"?>
<SAMLConfiguration xmlns="urn:componentspace:SAML:2.0:configuration">
<IdentityProvider Name="https://TestSaml"
       Description="Test Identity Provider"       
       LocalCertificateFile="Certificates\ca.key"
       LocalCertificatePassword="readycapital"/>

<PartnerServiceProviders>
  <!-- MVC example -->
  <PartnerServiceProvider Name="urn:oasis:names:tc:SAML:2.0:assertion"
          Description="MVC Example Service Provider"          
          SignSAMLResponse="true"
          SignAssertion="false"
          EncryptAssertion="true"
          AssertionConsumerServiceUrl="http://www.paybill.com/V2/Test/Login.aspx"          
          PartnerCertificateFile="Certificates\btat2.cert"/>
</PartnerServiceProviders>
</SAMLConfiguration>

控制器

public ActionResult Index(Profile profile)
   {
    string targetUrl = WebConfigurationManager.AppSettings["TargetURL"];
    string userName = "00373219101";// WebConfigurationManager.AppSettings["SubjectName"]; 
    SAMLAttribute[] attributes = new SAMLAttribute[2];
    SAMLAttribute attribute = new SAMLAttribute("UserEmailAddress", SAMLIdentifiers.AttributeNameFormats.Unspecified, null, string.Empty);
    attributes[0] = attribute;

    SAMLAttribute attribute2 = new SAMLAttribute("MiscellaneousData", SAMLIdentifiers.AttributeNameFormats.Unspecified, null, string.Empty);
    attributes[1] = attribute2;

    SAMLIdentityProvider.InitiateSSO(Response, userName, attributes, targetUrl);
}
  • 您是否检查过 WebServer 是否可以实际访问这些文件?也许使用 Microsoft Windows Sysinternals Process Monitor 并检查读取操作是否成功。

  • 将独立的 .key 文件替换为包含证书和私钥的 .pfx 文件,link 替换为 IdentityProvider/@LocalCertificateFile