【问题标题】:ASP.NET - The specified network password is not correctASP.NET - 指定的网络密码不正确
【发布时间】:2012-01-07 08:06:09
【问题描述】:

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

[CryptographicException: The specified network password is not correct.]

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

即使网络之间没有密码,也没有证书密码。 (我知道是因为开发人员无需密码即可工作)。 我拥有的唯一密码是与 DEV 相同的 WCF 密码。

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); 

这是完整的堆栈

[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

谢谢

【问题讨论】:

标签: c# .net certificate


【解决方案1】:

我也遇到了 pfx 文件的问题,问题是它是使用 AES256-SHA256 加密导出的,这引发了与问题中相同的异常。根据this 的说法,AES256-SHA256 仅在 Windows 10 1703、Windows Server 2016 及更高版本上支持。 更改为 TripleDES-SHA1“解决了”问题。

【讨论】:

  • 另见:link
  • 如果其他人遇到这个问题,试图在 azure 门户上加载这样的 AES256 加密证书,或者通过文件/字节数组中的代码加载......我得到了同样的(误导性)错误,我只解决了在 3DES-SHA1 中重新导出证书的问题。这很奇怪,因为它在本地可以工作,但是一旦在 webapp 上发布就停止工作...... PS:在此之前,我也尝试过缩短密码,但它没有解决任何问题。
【解决方案2】:

我在设置了 40 个字符的密码的 PFX 证书文件中遇到了这个问题,Windows 允许我将它导出并导入它,但它不能在 C# 代码中使用,所以我更改为 30 个字符密码就可以了。

【讨论】:

    【解决方案3】:

    试试这个:

    new X509Certificate2(Path, "", X509KeyStorageFlags.MachineKeySet); 
    

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

    【讨论】:

    • 如果设置 X509KeyStorageFlags 参数,每次实例化 X509Certificate2 对象时,它都会在 C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 文件夹中创建一个密钥文件,如果你经常这样做,你可以结束已填满磁盘,因此请谨慎使用。
    • 对我来说最好的解决方案是让asp加载用户配置文件。检查stackoverflow.com/a/10048789/356604
    • 这非常有效。如果您有密码并想申请,则必须以这种方式运行 X509Certificate2 certificate = new X509Certificate2(Path, "yourpassword", X509KeyStorageFlags.MachineKeySet);
    • 只想提一下新的 X509Certificate2(Path, "your-password", X509KeyStorageFlags.MachineKeySet);
    • 可能是X509KeyStorageFLags.EphemeralKeySet?
    猜你喜欢
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2016-06-12
    • 2022-12-17
    • 2015-09-02
    • 2020-07-11
    • 2016-05-03
    相关资源
    最近更新 更多