Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pass IEnumerable KeyValuePair into String function

Posted on 2014-11-15
4
Medium Priority
?
459 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

In the wake of AWS' S3 outage, we want to discuss the importance of storage and data diversification in the event of a hack, crash, or system disruption. We spoke with Experts Exchange’s COO Gene Richardson for a deeper understanding.
When speed and performance are vital to revenue, companies must have complete confidence in their cloud environment.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

971 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