Windows service serviceCredentials 无效的十六进制字符串格式
Windows service serviceCredentials Invalid hexadecimal string format
我正在调试 Win 服务。我在服务器上添加了一些证书。试图通过序列号查找证书。
https://gyazo.com/9cdcda75e98fe7b7c35496976a5aaaeb
behaviors.config的片段:
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="DDBS.DDBSPasswordValidator, DDBS" />
<serviceCertificate findValue="1C5411F9D38252824C2EC1CC7E5EBE3F" x509FindType="FindBySerialNumber" storeLocation="LocalMachine" storeName="My" />
</serviceCredentials>
我收到错误消息:无法启动服务。 System.FormatException: 无效的十六进制字符串格式。
使用 FindBySubjectName 选项查找。服务很好
来自 MSDN Forum 上的 Jan Angelovič:
If you cut and paste the [Serial Number or Thumbprint]
from the common certificate dialog, it is copied in UNICODE
(UTF-16LE) encoding with special invisible control characters
(0xFF and 0xFE aka “Byte order mark“) on the beginning of the
string.
就个人而言,我是用指纹看到的;从对话框中剪切并粘贴到 Visual Studio 中保留了 2 个未显示的字符(可能是 "stepped through" 使用光标并删除)。
删除所有 findValue="XXXXXXX" 然后再写一遍..有时无效的字符在 findValue="??? <--- 这里
使用 OpenSSL 实用程序 生成证书然后在 生产服务器 上安装后,我遇到了同样的问题。
问题出在我检查 生产服务器 上的证书序列号时
它显示带空格的十六进制数,这是导致错误的原因。
对我有用的解决方案 是首先在您的机器上导入相同的证书检查其序列号(在我的情况下它没有空格值)然后我复制了该值并粘贴到 Production 上然后它起作用了。
希望这会有所帮助。
通过对@richaux 的解决方案进行轻微添加来解决。 “无效的十六进制字符串格式。” 异常即使在删除空格或手动键入指纹后仍然存在。我将代码行从 VS 2019 复制到 Notepad++,这就是它的样子
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.Root, X509FindType.FindByThumbprint, "0f12c34c56fa78901234567e890af123ca45678d");
注意值字符串末尾的字符。此字符在 Visual Studio 中不可见。我在 Notepad++ 中删除了这个字符,并将从 Notepad++ 复制的行粘贴到 VS 2019 中。编译和 运行 此更新代码删除了 "无效的十六进制字符串格式。" 异常。
我正在调试 Win 服务。我在服务器上添加了一些证书。试图通过序列号查找证书。 https://gyazo.com/9cdcda75e98fe7b7c35496976a5aaaeb
behaviors.config的片段:
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="DDBS.DDBSPasswordValidator, DDBS" />
<serviceCertificate findValue="1C5411F9D38252824C2EC1CC7E5EBE3F" x509FindType="FindBySerialNumber" storeLocation="LocalMachine" storeName="My" />
</serviceCredentials>
我收到错误消息:无法启动服务。 System.FormatException: 无效的十六进制字符串格式。
使用 FindBySubjectName 选项查找。服务很好
来自 MSDN Forum 上的 Jan Angelovič:
If you cut and paste the [Serial Number or Thumbprint] from the common certificate dialog, it is copied in UNICODE (UTF-16LE) encoding with special invisible control characters (0xFF and 0xFE aka “Byte order mark“) on the beginning of the string.
就个人而言,我是用指纹看到的;从对话框中剪切并粘贴到 Visual Studio 中保留了 2 个未显示的字符(可能是 "stepped through" 使用光标并删除)。
删除所有 findValue="XXXXXXX" 然后再写一遍..有时无效的字符在 findValue="??? <--- 这里
使用 OpenSSL 实用程序 生成证书然后在 生产服务器 上安装后,我遇到了同样的问题。 问题出在我检查 生产服务器 上的证书序列号时 它显示带空格的十六进制数,这是导致错误的原因。
对我有用的解决方案 是首先在您的机器上导入相同的证书检查其序列号(在我的情况下它没有空格值)然后我复制了该值并粘贴到 Production 上然后它起作用了。
希望这会有所帮助。
通过对@richaux 的解决方案进行轻微添加来解决。 “无效的十六进制字符串格式。” 异常即使在删除空格或手动键入指纹后仍然存在。我将代码行从 VS 2019 复制到 Notepad++,这就是它的样子
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.Root, X509FindType.FindByThumbprint, "0f12c34c56fa78901234567e890af123ca45678d");
注意值字符串末尾的字符。此字符在 Visual Studio 中不可见。我在 Notepad++ 中删除了这个字符,并将从 Notepad++ 复制的行粘贴到 VS 2019 中。编译和 运行 此更新代码删除了 "无效的十六进制字符串格式。" 异常。