I have a program that submits data to a third party web service. They are implementing two-factor authentication where I have to create a nonce key (using NewGuid), they provide an encoded API key to include, as well as a screen name, URL and time stamp.
I have the nonce being created, and then using the api key, I sign the data and in the end, I'll have a long string such as this:
2017-04-15T17:08:57Z-1265fb1e-bbc3-453a-be409e2a808cbaaeWpyp6EJnIUlSX1rB/YRJxRyp8cXxw2IIrFMnnvuB06cUBabyRLnD 5hPj+ndH8zSIhojvNgc10/az2N+hh6SaMA==
It states that it would be sent in an HTTP header called X-WME-API-Token.
We have a MeterDataService.cs file that was generated from the SVCUTIL command line tool that creates a proxy client. I don't see anywhere in there that I can add the customer header.
I know that you can do something like this if you have something derived from WebRequest:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urlString);
req.ContentType = "text/xml";
req.Headers["X-SPP-API-Token"] = Token;
req.Method = "POST";
However, none of the resulting objects is derived from WebRequest, so I'm not sure how to do this.
One object, the MeterDataPortClient object, has an Endpoint property. Is that where I would do it? How do I do that? I guess that's my question.
I did try modifying the MeterDataRequest object they created by having it inherit from WebRequest, but then I got this error:
"The type PostMeterDataRequest defines a MessageContract but also derives from a type System.Net.WebRequest that does not define a MessageContract. All of the objects in the inheritance hierarchy of PostMeterDataRequest must defines a MessageContract."
Here's the the definition of the PostMeterDataRequest and I see the MessageContractAttribute being applied to the class. I'm just not familiar with what that means. I just need to find a way to add the HTTP header to the request object before it's sent to the web service since the service at the receiving end expects the token to be in the HTTP header and I have no idea how to put it in with the code that I have at hand.
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped = false)]
public partial class PostMeterDataRequest : WebRequest // WebRequest added my me later on.
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://someorg.com/schema/MeterDataSchema/v2", Order = 0)]
public PostMeterDataType PostMeterData;
public PostMeterDataRequest()
{
}
public PostMeterDataRequest(PostMeterDataType PostMeterData)
{
this.PostMeterData = PostMeterData;
}
}