Link to home
Start Free TrialLog in
Avatar of Michael Krumpe
Michael KrumpeFlag for United States of America

asked on

Pass IEnumerable KeyValuePair into String function

I am constructing a REST Client that needs to authenticate a string on every post as a signature. Within that signature I have to Canonicalize the Headers and string variables that are being sent, sort them, then out put them in a format to then do a SHA256 hash on the value.

Ok, I have the String function that Canolicalizes the headers and does the sort with a bit of Linq, I also have the SHA covered.
In my String function shown below, I am expecting to pass into it an IEnumberable key value pair, as defined by an inheritance. So my question is, how can I pass an IEnumberable collection into the String Function when calling it from another class?

The reason I have to have it as a function of its own, because as it is REST, depending on the endpoint and resource I am calling for, I can have different values in the collection. Second snippet of code is an example of that collection, which yes I can create the collection - but do not know if I am constructing it or passing it into my CanonicalizedHeaders() string function correctly.

public interface CanoHeaders
        {
            // Structure for CanonicalizedHeaders List
            string pname { get; set; }
            string pvalue { get; set; }
        }


public string CanonicalizedHeaders(IEnumerable<CanoHeaders> CanonicalHeader)

            CanonicalHeader.OrderBy(x => x.pname);
            var listHeaders = from CanoHeaders in CanonicalHeader
                              select new { param = CanoHeaders.pname, value = CanoHeaders.pvalue };

           string returnheaders = String.Empty;

            foreach (var row in listHeaders)
            {
                returnheaders += row.param.ToString() + ":" + row.value.ToString() + "\n";
            }

            return returnheaders.ToLower().ToString();
        {

Open in new window


This is the piece that is likely incorrect at the string cano_str (being called by a seperate class)

ICollection<KeyValuePair<String, String>> vmCollec = new Dictionary<String, String>();
                vmCollec.Add(new KeyValuePair<String, String>("offset", str_offset));
                vmCollec.Add(new KeyValuePair<String, String>("limit", str_limit));

            string cano_str = CanonicalizedHeaders(vmCollec.AsEnumerable);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Michael Krumpe

ASKER

Is there another was of passing the values in, not necessarily as a kvp? Would any kind of collection suffice?
@kaufmed - this is perfect, thank you very much. I figured there needed to be a tweak in how I was going about it. Seems like fundamentally I was there, but you helped pull it in. Thanks again!
Thank you very much - your code was completely what I needed.