免責聲明

Disclaimer (免責聲明)
繼續閱覽代表您接受以上的免責聲明.
To continue reading means you accept the above disclaimer.

2010年6月7日 星期一

Get CN from Certificate

[Q] How to get "CN" attribute from a certificate ?

X509Certificate2 cert;
...
cert.SubjectName.Name= "CN= ..., OU= ..."

[try] my primitive approach ...
//=== C# code snippet


public string getCN( X509Certificate2  cert)
{
string s = cert.SubjectName.Name;
string[] strArr=s.Split(new char[] { ',' });

for (int i = 0; i < strArr.Length; i++)
{
  if (strArr[i].Contains("CN"))
  {
    s = strArr[i];
    int pos = s.IndexOf('=');
    s=s.Substring(pos + 1);
    break;
   }

 }

 return s
}

沒有留言:

張貼留言