X509Store 인증서 관련 문제. FindByThumbprint 찾기
방법을 사용할 때 문제가 있습니다. X509Store.Certificates.Find
public static X509Certificate2 FromStore(StoreName storeName,
StoreLocation storeLocation, X509FindType findType, string findValue)
{
X509Store store = new X509Store(storeName, storeLocation);
store.Open(OpenFlags.ReadOnly);
try
{
//findValue = "7a6fa503ab57b81d6318a51ca265e739a51ce660"
var results = store.Certificates.Find(findType, findValue, true);
return results[0];
}
finally
{
store.Close();
}
}
이 경우 Find 메서드는 0 개의 결과 ( results.Count == 0)를 반환 하지만 findValue를 상수로 지정하면 메서드가 인증서를 찾습니다.
public static X509Certificate2 FromStore(StoreName storeName,
StoreLocation storeLocation, X509FindType findType, string findValue)
{
X509Store store = new X509Store(storeName, storeLocation);
store.Open(OpenFlags.ReadOnly);
try
{
//findValue= "7a6fa503ab57b81d6318a51ca265e739a51ce660"
var results = store.Certificates.Find(findType,
"7a6fa503ab57b81d6318a51ca265e739a51ce660", true);
return results[0];
}
finally
{
store.Close();
}
}
Windows 인증서 정보 대화 상자의 지문을 코드 (또는 간단한 예제 인 경우 구성 파일)에 복사하여 붙여 넣었다고 가정합니다. 귀찮게도 지문 텍스트 상자의 첫 번째 문자는 보이지 않는 유니 코드 "왼쪽에서 오른쪽으로 표시"제어 문자 입니다. 여는 문자열 따옴표와 손도장의 첫 번째 문자를 선택하여 삭제 한 다음 (사이에 보이지 않는 문자도 제거됨) 손으로 다시 입력 해보십시오.
나는 오늘이 이상한 행동을 당했고 그것을 알아내는 데 한 시간 넘게 걸렸다. 나는 마침내 그것을보고하는 방법은의 길이와 해시 코드를 확인하기 위해 디버거를 사용하여이었다 findValue과의 Thumbprint서로 다른 것으로 밝혀졌다 인증서 객체의를. 이로 인해 보이지 않는 문자가 나타난 디버거에서 해당 문자열의 문자 배열을 검사하게되었습니다.
여기에 몇 가지 답변을 가져와 특수 문자와 대문자를 모두 제거하는 정적 메서드로 결합했습니다. 다른 사람이 사용할 수 있기를 바랍니다.
public static X509Certificate2 GetCertificate(string thumbprint)
{
// strip any non-hexadecimal values and make uppercase
thumbprint = Regex.Replace(thumbprint, @"[^\da-fA-F]", string.Empty).ToUpper();
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
try
{
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates;
var signingCert = certCollection.Find(X509FindType.FindByThumbprint, thumbprint, false);
if (signingCert.Count == 0)
{
throw new FileNotFoundException(string.Format("Cert with thumbprint: '{0}' not found in local machine cert store.", thumbprint));
}
return signingCert[0];
}
finally
{
store.Close();
}
}
나는 같은 문제가 있었고 그것을 해결했습니다.
mmc에서 VS로 직접 지문을 복사했습니다. 나는 문자열을 비교했지만 차이를 찾지 못했습니다.
hash.length로 길이를 확인하면 41 대 40의 차이가 있습니다.
mmc에서 복사하여 문자열에 추가 된 보이지 않는 Char가 있습니다.
해결 :
- mmc에서 Notepad.exe로 지문 복사
- 이 문자열을 다시 복사
- 코드에 붙여 넣기
작동합니다.
나는 이것에 희생되었습니다. Windows 콘솔 스냅인 지문 표시에 유니 코드 "왼쪽에서 오른쪽"문자가있을뿐만 아니라 두 문자 사이에 공백이있는 소문자 16 진수 문자도있었습니다. CertUtil의 출력에도 소문자와 공백이 있습니다. 일치를 얻으려면 findValue를 다음으로 변환 된 문자열로 지정해야했습니다.
- 선행 특수 문자를 제거하고
- 문자 클러스터 사이의 공백을 제거하고,
- 모든 문자를 대문자로 변경하십시오 .
이로 인해 MMC에서 복사하여 붙여 넣을 때 지문을 정리하는이 함수를 작성했습니다.
public string CleanThumbprint(string mmcThumbprint)
{
//replace spaces, non word chars and convert to uppercase
return Regex.Replace(mmcThumbprint, @"\s|\W", "").ToUpper();
}
...
var myThumbprint = CleanThumbprint("b3 ab 84 e5 1e e5 e4 75 e7 a5 3e 27 8c 87 9d 2f 05 02 27 56");
var myCertificate = certificates.Find(X509FindType.FindByThumbprint, myThumbprint, true)[0];
이 코드는 작동합니다.
I suppose you have copied this thumbprint from the certificate management console. And that copied value contains unicode non-readable symbol which is invisible in Visual Studio. Try to delete the first invisible symbol and if this is what I think of, this should work.
Replace the code to find your certificate in the store as below:
var results = store.Certificates.Find(findType, findValue, true);
Also the 3rd param which is bool return certificates only if the certificate is valid. So make sure that your certificate is valid. If you have a self signed certificate or so then just pass the 3rd param to be "false"
I ran into this same thing. I couldn't find this answer anywhere in here so I'll post it. It seems for me the X509Store find function just was flat not working. I verified this by a simple for loop and retrieving the cert manually.
X509Store store = new X509Store(StoreName.Root,StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate cert = new X509Certificate();
for (int i = 0; i < store.Certificates.Count; i++)
{
if (store.Certificates[i].SerialNumber == "XXXX")
{
cert = store.Certificates[i];
}
}
var results = store.Certificates.Find(findType, findType, true);
I think you mean the 2nd param to be "findValue".
Here is the simple version of code for the above suggestions- ofcourse which is worked for me
private X509Certificate2 GetCertificate()
{
var certStore = new X509Store("my");
certStore.Open(OpenFlags.ReadOnly);
try
{
const string thumbprint = "18 33 fe 3a 67 d1 9e 0d f6 1e e5 d5 58 aa 8a 97 8c c4 d8 c3";
var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint,
Regex.Replace(thumbprint, @"\s+", "").ToUpper(), false);
if (certCollection.Count > 0)
return certCollection[0];
}
finally
{
certStore.Close();
}
return null;
}
I encounter this invisible Unicode char as well. Trying using Notepad (Windows 10) somehow didn't work well for me either. Finally, I use PowerShell to get the clean thumbprint hex:
PS C:\> $tp= (Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match "mycert"}).Thumbprint;
PS C:\> $tp
SO much for Unicode char.
Just to let you know what the invisible character is, I see the thumbprint in the mmc being: 75 3a ...
Then I copy and paste it in my vim, I see the following:
<200e>75 3a ...
So after you get rid of the first char "<200e>" and the extra spaces, you'll be fine.
'programing tip' 카테고리의 다른 글
| Python에서 dict의 사전을 초기화하는 가장 좋은 방법은 무엇입니까? (0) | 2020.10.09 |
|---|---|
| 리팩토링 목적으로 속성 만있는 클래스를 갖는 것이 괜찮습니까? (0) | 2020.10.09 |
| 이미 컴파일 된 바이너리에서 'rpath'를 변경할 수 있습니까? (0) | 2020.10.09 |
| OS X 용 lsusb와 동등한 기능이 있습니까? (0) | 2020.10.09 |
| moment.js, 요일 번호를 얻는 방법 (0) | 2020.10.09 |