Solved

Pass IEnumerable KeyValuePair into String function

Posted on 2014-11-15
4
417 Views
Last Modified: 2016-02-16
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
Comment
Question by:Michael Krumpe
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40445801
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
 
LVL 4

Author Comment

by:Michael Krumpe
ID: 40446705
Is there another was of passing the values in, not necessarily as a kvp? Would any kind of collection suffice?
0
 
LVL 4

Author Comment

by:Michael Krumpe
ID: 40448197
@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
 
LVL 4

Author Closing Comment

by:Michael Krumpe
ID: 40448212
Thank you very much - your code was completely what I needed.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
Do you know what to look for when considering cloud computing? Should you hire someone or try to do it yourself? I'll be covering these questions and looking at the best options for you and your business.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question