• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

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

0
Michael Krumpe
Asked:
Michael Krumpe
  • 3
1 Solution
 
käµfm³d 👽Commented:
You won't be able to do this because you have no way of tacking on your interface defintiion to a KeyValuePair (since you didn't write the KeyValuePair struct). Why not create a small class which implements your interface? Then you could convert between the KVP and your new class--although, if you've got this new class, why even use KVP.

e.g.

using System;
using System.Collections.Generic;
using System.Linq;

namespace _28563059
{
    class Program
    {
        static void Main(string[] args)
        {
            string str_offset = "";
            string str_limit = "";
            ICollection<KeyValuePair<String, String>> vmCollec = new Dictionary<String, String>();
            var d = new { Name = "Ja" };

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

            string cano_str = CanonicalizedHeaders(vmCollec.Select(kvp => new CanonicalizedHeaders() { pname = kvp.Key, pvalue = kvp.Value }));
        }

        static 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();
        }
    }

    public class CanonicalizedHeaders : CanoHeaders
    {
        public string pname { get; set; }
        public string pvalue { get; set; }
    }

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

Open in new window

0
 
Michael KrumpeSolutions ArchitectAuthor Commented:
Is there another was of passing the values in, not necessarily as a kvp? Would any kind of collection suffice?
0
 
Michael KrumpeSolutions ArchitectAuthor Commented:
@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!
0
 
Michael KrumpeSolutions ArchitectAuthor Commented:
Thank you very much - your code was completely what I needed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now