尝试加载新的X509证书时找不到请求的对象

时间:2016-11-07 06:51:22

标签: c# .net x509certificate identityserver3

我正在浏览IdentityServer 3官方教程,我正在使用MVC应用教程(https://identityserver.github.io/Documentation/docsv2/overview/mvcGettingStarted.html),但我遇到了障碍。在“添加启动”部分,它可以让您下载要在应用程序中使用的证书,但我无法使其工作。

本地失败的示例代码是:

X509Certificate2 LoadCertificate()
    {
        return new X509Certificate2(
            string.Format(@"{0}\bin\identityServer\idsrv3test.pfx", AppDomain.CurrentDomain.BaseDirectory), "idsrv3test");
    }

但是,尽管证书在这个位置,我收到一个错误。我甚至尝试将证书复制到临时文件夹,所以我知道路径很好,

X509Certificate2 LoadCertificate()
        {
            return new X509Certificate2("c:\\temp\\idsrv3test.pfx", "idsrv3test");
        }

但它仍然失败并显示相同的消息:

Cannot find the requested object.

  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.Security.Cryptography.CryptographicException: Cannot find the requested object.


Source Error: 



Line 33:         X509Certificate2 LoadCertificate()
Line 34:         {
Line 35:             return new X509Certificate2("c:\\temp\\idsrv3test.pfx", "idsrv3test");
Line 36:         }
Line 37:     }

堆栈追踪:

[CryptographicException: Cannot find the requested object.
]
   System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +36
   System.Security.Cryptography.X509Certificates.X509Utils._QueryCertBlobType(Byte[] rawData) +0
   System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags) +66
   System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password) +109
   is3_mvc.Startup.LoadCertificate() in C:\scratch\.net\IdentityServer3\is3-mvc\is3-mvc\Startup.cs:59
   is3_mvc.Startup.<Configuration>b__4_0(IAppBuilder idsrvApp) in C:\scratch\.net\IdentityServer3\is3-mvc\is3-mvc\Startup.cs:25
   Owin.MapExtensions.Map(IAppBuilder app, PathString pathMatch, Action`1 configuration) +178
   Owin.MapExtensions.Map(IAppBuilder app, String pathMatch, Action`1 configuration) +108
   is3_mvc.Startup.Configuration(IAppBuilder app) in C:\scratch\.net\IdentityServer3\is3-mvc\is3-mvc\Startup.cs:23

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +160
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +101
   Owin.Loader.<>c__DisplayClass12.<MakeDelegate>b__b(IAppBuilder builder) +66
   Owin.Loader.<>c__DisplayClass1.<LoadImplementation>b__0(IAppBuilder builder) +123
   Microsoft.Owin.Host.SystemWeb.<>c__DisplayClass2.<InitializeBlueprint>b__0(IAppBuilder builder) +71
   Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action`1 startup) +462
   Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action`1 startup) +40
   Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() +70
   System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +115
   Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) +106
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +536
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Exception has been thrown by the target of an invocation.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9963380
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

我也尝试使用不同的方法加载证书:

var stx = File.Open(@"c:\temp\idsrv3test.pfx", FileMode.Open);
            using (BinaryReader br = new BinaryReader(stx))
            {
                return new X509Certificate2(br.ReadBytes((int)br.BaseStream.Length), "idsrv3test");
            }

但是这给出了完全相同的错误,因此我认为加载文件本身并不存在问题。

有没有人遇到过这个问题?似乎其他人之前在SO上遇到过类似的问题,但没有发布与上述相符的确切解决方案。

我猜这不是文件的实际定位问题,而是其他问题,但我不知道那可能是什么......

由于

0 个答案:

没有答案