Solved

Pass IEnumerable KeyValuePair into String function

Posted on 2014-11-15
4
392 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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
Thank you very much - your code was completely what I needed.
0

Featured Post

How to Backup Ubuntu to Amazon S3

CloudBerry Backup offers automatic cloud backup and restoration for Linux. It has both GUI and command line interface (CLI) ensuring its flexibility in use. Find out more

Join & Write a Comment

Outsource Your Fax Infrastructure to the Cloud (And come out looking like an IT Hero!) Relative to the many demands on today’s IT teams, spending capital, time and resources to maintain physical fax servers and infrastructure is not a high priority.
Big data transfers via information superhighways require special attention and protection. Learn more about the IT-regulations of the country where your server is located. Analyze cloud providers and their encryption systems for safe data transit. S…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This Micro Tutorial will explain how to export DynamoDB tables in Amazon Web Services.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now