证书生成、应用及常见错误处理

一、生成证书
使用管理员登录,执行下面的命令 导入证书

makecert -r -pe -n "CN=127.0.0.1"  -sr LocalMachine -ss My -sky exchange

提示成功后表明证书已生成。

如果没有这个makercert.exe,从WIN7机器中拷贝一个到服务器(比如win2008)也可以。
理论上,这个exe可以存放在任意地方

证书生成之后,怎么看?可以这样:
运行 mmc 命令
这里写图片描述
添加证书管理单元
这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

二、证书在WCF上的应用
证书与WCF,这两坨都是我所讨厌的。现在它们混成一伙,狼狈为奸了。尤其是WCF,垃圾。垃圾中的战斗圾。搞出来就是为了恶心人。

1、在应用WCF的项目的web.config加入以下类似代码。比如这是一个文件服务器项目。

<system.serviceModel>
<bindings>
  <wsHttpBinding>
    <binding name="wsHttpEndpointBinding" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">
      <security>
        <message clientCredentialType="UserName" />
      </security>
      <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" maxBytesPerRead=" 2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </wsHttpBinding>
</bindings>
<services>
  <service name="SHIT.FileServer.FileProService" behaviorConfiguration="SHIT.FileServer.FileProServiceBehavior">
    <endpoint address="" binding="wsHttpBinding" name="username" contract="SHIT.FileServer.IFileProService" bindingConfiguration="wsHttpEndpointBinding">
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="SHIT.FileServer.FileProServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceCredentials>
        <issuedTokenAuthentication allowUntrustedRsaIssuers="true"></issuedTokenAuthentication>
        <serviceCertificate findValue="127.0.0.1" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="SHIT.Core.WCF.LtNamePasswordValidator,SHIT.Core" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

谁能看懂这一大坨是个什么鬼。不过里面有一个”127.0.0.1”与上面生成证书的中命令参数相关。

makecert -r -pe -n "CN=127.0.0.1"  -sr LocalMachine -ss My -sky exchange

2、在客户端,凭用户名和密码来访问这个WCF服务:

string httpIP = "127.0.0.1";
if (string.IsNullOrEmpty(httpIP)) throw new Exception("请配置文件上传服务地址参数:FileService");
EndpointAddress httpAddress = new EndpointAddress(httpIP);
_FileService = new FileProServiceClient("FileProService", httpAddress);
_FileService.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
_FileService.ClientCredentials.UserName.UserName ="test" ;
_FileService.ClientCredentials.UserName.Password = "123456";
return _FileService;

三、如果提示“密钥集不存在(The process must have access rights for the private key)”
解决办法:
C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA将这个目录下的MachineKeys文件夹添加Everyone并赋予浏览权限

四、提示搜索到多个证书
如果手贱生成了多个证书,则可以删掉一些。方法可以参照 一。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你遇到 "Invalid keystore format" 错误时,通常是因为你的密钥库文件格式不正确导致的。密钥库文件通常用于存储加密密钥和证书,以便在应用程序中进行安全通信。 这个错误可能有以下几个原因和解决方法: 1. 密钥库文件格式错误:请确保你使用的密钥库文件是正确的格式。常见的密钥库格式包括 JKS (Java KeyStore) 和 PKCS12。如果你使用的是 JKS 格式,可以使用以下命令检查密钥库文件: ``` keytool -list -v -keystore your_keystore_file.jks ``` 如果你使用的是 PKCS12 格式,可以使用以下命令检查密钥库文件: ``` keytool -list -v -storetype PKCS12 -keystore your_keystore_file.p12 ``` 如果命令执行成功并显示密钥库文件的详细信息,则说明密钥库文件格式正确。 2. 密钥库密码错误:密钥库文件通常需要设置一个密码来保护其中的密钥和证书。请确保你输入的密码与密钥库文件的密码一致。如果你不确定密码是否正确,可以尝试使用以下命令更改密钥库文件的密码: ``` keytool -storepasswd -keystore your_keystore_file.jks ``` 或者 ``` keytool -storepasswd -storetype PKCS12 -keystore your_keystore_file.p12 ``` 根据密钥库文件的格式选择相应的命令。 3. 密钥库文件损坏:如果以上方法都无法解决问题,可能是密钥库文件本身损坏了。你可以尝试使用备份的密钥库文件,或者重新生成一个新的密钥库文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值