Reputation: 4733
There is a string that has the following key value pair. It is being Seperated by "," So after splitting it by.
string[] strSubject = certificate.Subject.Split(',');
[0] "[email protected]"
[1] " CN=Ankur"
[2] " OU=Telco"
[3] " O=aol"
[4] " L=Mum"
[5] " S=Mh"
[6] " C=IN"
the value is a combination of key value pair, With key name fixed. The problem is it is not mandatory to have all the keys for all objects . what it meant is for some object it will have some values, for Example
[0] "[email protected]"
[1] " CN=Ankur"
[2] " S=Mh"
[3] " C=IN"
So I cant hard code the Index and fetch the value by doing some thing like this
String value = strSubject[5].Replace("S=",string.Empty));
I have tried using .contains too like this
if (strSubject.Contains("C="))//Don't Know the value part as it is dynamic
{
//this does not work
}
How can I get the values of the keys that are present after splitting . Can Linq query fetch the value not sure about it.
Upvotes: 3
Views: 3237
Reputation: 2986
Parsing distinguished names can be challenging. I suggest you consider Hall72215's answer here, or this project on code project for a solution in managed code.
The accepted answer by faester will break if you have RDNs with the same name which is quite common for distinguished names. For example this certificate from Verisign:
OU = NO LIABILITY ACCEPTED, (c)97 VeriSign, Inc., OU = VeriSign Time Stamping Service, Root OU = VeriSign, Inc., O = VeriSign Trust Network
Here you have two occurences of OU, and one of the OU values contains a comma which will also break the code.
Upvotes: 0
Reputation: 15086
Create a dictionary and query that:
var testStr = "E=foo,B=Bar,O=aol,FAIL";
var dict = testStr.Split(',').Where(x => x.Contains("="))
.Select(x => x.Trim().Split('=')).ToDictionary(x => x[0], x=> x[1]);
dict.ContainsKey("theKey");
(This also gives you a convenient way of handle the queries.)
Upvotes: 2
Reputation: 244878
What you should do is to parse the string into something like Dictionary<string, string>
, it will be much easier to work with that.
Something like (assuming the space after ,
is required):
var subjectDictionary = certificate.Subject
.Split(new[] { ", " }, StringSplitOptions.None)
.Select(pair => pair.Split('='))
.ToDictionary(pair => pair[0], pair => pair[1])
if (subjectDictionary.ContainsKey("C"))
{
var valueForC = subjectDictionary["C"];
…
}
Upvotes: 2
Reputation: 14432
To get the values just use this line of code:
var values = strSubject.Select(v => v.Split('=')[1]);
And for key and value:
var values = strSubject.Select(v => new { Key = v.Split('=')[0], Value = v.Split('=')[1] });
Upvotes: 0
Reputation: 1161
I am not sure what exactly is your aim, but what if you do a Split again with '=' as a separator:
string[] strParts = theValue.Split("=");
Than you have the key and the value in strParts[0]
and strParts[1]
respectively.
Upvotes: 0
Reputation: 13600
I guess something like this should work..
string[] strSubject = certificate.Subject.Split(',');
for (int i = 0; i < strSubject.Length; i++)
{
string[] row = strSubject[i].Split('=');
switch (row[0]) // the key
{
case "E":
// do something, value is stored in row[1]
break;
case "CN":
// do something else
break;
//... cover the expected keys
}
}
Upvotes: 1
Reputation: 8868
Try get all the key and values:
var q = certificate.Subject.Split(',')
.Select(x => x.Trim())
.Select(x => new
{
key = x.Substring(0, x.IndexOf('=')).Trim(),
value = x.Substring(x.IndexOf('=') + 1).Trim()
});
then:
foreach (var x in q)
{
if (x.key == "C")
{
// code
}
}
Upvotes: 0
Reputation: 14470
for(var i=0;i<strSubject.Length;i++)
{
if(strSubject[i].Trim().StartsWith("C="))
{
//perform your action
}
}
Upvotes: 0
Reputation: 2547
I like to use the yourWord.IndexOf(inputString) != 0
So using System.Linq
var match = yourWords.ToList().FirstOrDefault(x => x.ToLower().IndexOf(inputString) != 0);
But there are loads of ways of other ways doing this. Pick your fave!
Upvotes: 0