Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pass IEnumerable KeyValuePair into String function

Posted on 2014-11-15
4
Medium Priority
?
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

Question has a verified solution.

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

When speed and performance are vital to revenue, companies must have complete confidence in their cloud environment.
Considering cloud tradeoffs and determining the right mix for your organization.
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…
Both in life and business – not all partnerships are created equal. Spend 30 short minutes with us to learn:   • Key questions to ask when considering a partnership to accelerate your business into the cloud • Pitfalls and mistakes other partners…

688 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