ASP.NET - 指定的网络密码不正确

Sex*_*yMF 19 .net c# certificate

我在我的开发机器上有一个需要证书的WCF客户端,它工作正常.
部署到生产服务器后,我收到以下错误:

[CryptographicException: The specified network password is not correct.]
Run Code Online (Sandbox Code Playgroud)

DEV - Win7 32BIT IIS 7.5
生产 - Win SERVER 64BIT 2008 IIS 7.5

即使网络之间没有密码也没有证书密码.(我知道因为开发工作没有密码).我唯一的密码是WCF,它与DEV相同.

CrmServiceClient crm = new CrmServiceClient("CrmServiceEndpoint");
crm.ClientCredentials.UserName.UserName = CrmConfigRepository.CrmUserName;//fine
crm.ClientCredentials.UserName.Password = CrmConfigRepository.CrmPassword;//fine
crm.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(Path);
///THIS WONT WORK AS WELL
crm.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(Path, "", X509KeyStorageFlags.Exportable); 
Run Code Online (Sandbox Code Playgroud)

这是完整的堆栈

[CryptographicException: The specified network password is not correct. ]
   System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +41
   System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx) +0
   System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags) +372
   System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName) +101
   Externals.CrmConnection.Get() in C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\Externals\CrmConnection.cs:31
   ExpressBroker.Models.ActionsMetadata.Handlers.LeadAccountHandler.Handle(BrokerAction brokerAction, ActionStep step, Dictionary`2 httpPostDataCollection) in C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\ExpressBroker\Models\ActionsMetadata\Handlers\LeadAccountHandler.cs:45
   ExpressBroker.Models.ActionsMetadata.Handlers.BaseStepHandler.SecuredHandle(BrokerAction brokerAction, ActionStep step, Dictionary`2 httpPostDataCollection) in C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\ExpressBroker\Models\ActionsMetadata\Handlers\BaseStepHandler.cs:49
   ExpressBroker.Models.ActionsMetadata.Handlers.HandlerInvoker.Invoke(BrokerAction brokerAction, ActionStep actionStep, Dictionary`2 stepValues) in C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\ExpressBroker\Models\ActionsMetadata\Handlers\StepServerInoker.cs:29
   ExpressBroker.Controllers.LeadAccountController.Register(String step) in C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\ExpressBroker\Controllers\LeadAccountController.cs:28
   lambda_method(Closure , ControllerBase , Object[] ) +127
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +264
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +129
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +784922
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
   System.Web.Mvc.Controller.ExecuteCore() +159
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371
Run Code Online (Sandbox Code Playgroud)

谢谢

fat*_*zzy 50

试试这个:

new X509Certificate2(Path, "", X509KeyStorageFlags.MachineKeySet); 
Run Code Online (Sandbox Code Playgroud)

看起来X509Certificate2构造函数试图访问本地用户的私钥存储(即使加载PFX并且私钥在PFX中).使用asp.net时,通常不会加载用户配置文件,因此用户密钥库不存在.指定MachineKeySet会告诉构造函数查看始终存在的本地计算机密钥存储区.

  • 如果您设置X509KeyStorageFlags参数,它将在每次实例化X509Certificate2对象时在C:\ ProgramData\Microsoft\Crypto\RSA\MachineKeys文件夹中创建一个密钥文件,如果您经常这样做,最终可能会填满磁盘,因此请使用这个仔细. (5认同)
  • 对我来说最好的解决方案是让asp加载用户配置文件.请访问http://stackoverflow.com/a/10048789/356604 (3认同)
  • 这完美地工作。如果您有密码并想申请,则必须以这种方式运行 X509Certificate2 certificate = new X509Certificate2(Path, "yourpassword", X509KeyStorageFlags.MachineKeySet); (2认同)

小智 6

我还遇到了 PFX 文件的问题,问题是这样的:
使用AES256-SHA256加密导出(使用 Windows 11 PC),这引发了与在我的服务器上使用 PFX 时的问题相同的异常。这发生在我的 C# .Net 4.7 应用程序中,该应用程序在 Windows Server 2016 上运行。将导出更改为TripleDES-SHA1“解决”了该问题。显然 Windows Server 2016 不支持AES256-SHA256,但较新的版本支持。


小智 5

我也有一个 pfx 文件的问题,问题是,它是使用 AES256-SHA256 加密导出的,它抛出了与问题相同的异常。根据,AES256-SHA256仅支持Windows 10 1703和Windows Server 2016之上。更改为 TripleDES-SHA1“解决”了这个问题。