Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET-指定的网络密码不正确_C#_.net_Certificate - Fatal编程技术网

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

C# ASP.NET-指定的网络密码不正确,c#,.net,certificate,C#,.net,Certificate,我的开发机器中有一个WCF客户端,它需要证书,并且工作正常。 部署到生产服务器后,我出现以下错误: [CryptographicException: The specified network password is not correct.] DEV-Win7 32位IIS 7.5 生产-Win SERVER 64位2008 IIS 7.5 即使网络之间没有密码,也没有证书密码。(我知道,因为dev没有密码)。 我唯一拥有的密码是与开发人员相同的WCF密码 CrmServiceClient

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

[CryptographicException: The specified network password is not correct.]
DEV-Win7 32位IIS 7.5
生产-Win SERVER 64位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
[加密异常:指定的网络密码不正确。]
系统。安全。加密。加密异常。ThrowCryptographicException(Int32 hr)+41
System.Security.Cryptography.X509Certificates.X509Utils.\u LoadCertFromFile(字符串文件名、IntPtr密码、UInt32 dwFlags、布尔persistKeySet、SafeCertContextHandle和pCertCtx)+0
System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(字符串文件名、对象密码、x509keystrageFlags keystrageFlags)+372
System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(字符串文件名)+101
C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\Externals\CrmConnection.cs:31中的Externals.CrmConnection.Get()
C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\ExpressBroker\Models\ActionsMetadata.Handlers.LeadAccountHandler(BrokerAction BrokerAction,ActionStep,Dictionary`2 httpPostDataCollection)中的
C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\ExpressBroker\Models\ActionsMetadata.Handlers.BaseStepHandler.SecuredHandle(BrokerAction BrokerAction,ActionStep,Dictionary`2 httpPostDataCollection)中的
C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\ExpressBroker\Models\ActionsMetadata.Handlers.HandlerInvoker.Invoke(BrokerAction BrokerActionStep,ActionStep ActionStep,Dictionary`2 stepValues)中的
在C:\Users\avi\Documents\Visual Studio 2010\Projects\ExpressBroker\ExpressBroker\Controller\LeadAccountController.cs:28中注册(字符串步骤)
lambda_方法(闭包、控制器基、对象[])+127
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)+264
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)+39
System.Web.Mvc.c__显示类15.b__12()+129
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter筛选器、ActionExecutingContext预文本、Func`1 continuation)+784922
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext ControllerContext,IList`1筛选器,ActionDescriptor ActionDescriptor,IDictionary`2参数)+314
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext,String actionName)+784976
System.Web.Mvc.Controller.ExecuteCore()+159
System.Web.Mvc.ControllerBase.Execute(RequestContext)+335
System.Web.Mvc.c__显示类b.b__5()+62
System.Web.Mvc.Async.c__显示类1.b__0()+20
System.Web.Mvc.c_uuudisplayClasse.b_uuud()+54
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+453
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+371
谢谢

试试这个:

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

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

我在一个具有40个字符密码集的PFX证书文件中遇到了此问题,Windows允许我导出和导入它,但它不能用于C#代码,因此,我改为30个字符的密码,它可以工作。

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

X509Certificates构造函数中传递的路径是什么?我想是访问证书文件的网络密码。可能是重复的吗?如果设置X509KeystrageFlags参数,则每次实例化X509Certificate2对象时,它都会在C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys文件夹中创建一个密钥文件,如果经常这样做,可能会填满磁盘,因此请小心使用。对我来说,最好的解决方案是让asp加载用户配置文件。检查此功能是否正常工作。如果您有密码并希望应用,则必须按以下方式运行:X509Certificate2 certificate=new X509Certificate2(路径“yourpassword”,X509KeyStrageFlags.MachineKeySet);只想提及新的X509Certificate2(路径,“您的密码”,X509KeyStrageFlags.MachineKeySet);另请参阅:如果其他人试图在azure门户上加载这样一个AES256加密证书或通过代码从文件/字节数组加载,是否会遇到此问题。。。我得到了相同的(误导性)错误,我只解决了在3DES-SHA1中重新导出证书的问题。这很奇怪,因为它在本地工作,但在webapp上发布后停止工作。。。PS:在那之前,我也尝试过缩短密码,但没有解决任何问题。