2
Answers

WCF custom headers in .NET CF

Uros Bregar

Uros Bregar

14y
5.9k
1
Hello

I created a WCF service, which has a single endpoint and uses BasicHttpBinding, so it can be accessed by mobile clients. The service(for now) does not use security. I want to create an authentication mechanisem with CustomHeader-s. The client(a windows mobile device) calls a register method and adds a custom header with user credentials to the message. The service validates supplied credentials and returns a GUID - again in the CustomHeader. The problem is that I dot't know how to access the header on the client side. I tried to get header in the
 
private
TRESPONSE getResult<TRESPONSE>(System.ServiceModel.Channels.Message reply, CFInvokeInfo info)
{
}

method, where I make a call to the following method.
 

 
public
static string ReadHeader(Message request, XmlObjectSerializer serializer)
{
int headerIndex = request.Headers.FindHeader(HEADER_NAME, HEADER_NAMESPACE);
if (headerIndex != -1)
{
XmlNode[] contentNode = request.Headers.GetHeader<XmlNode[]>(headerIndex, serializer);
string text = contentNode[0].InnerText;
XmlSerializer deSerializer = new XmlSerializer(typeof(string));
StringReader reader = new StringReader(text);
string content = (string)deSerializer.Deserialize(reader);
reader.Close();
return content;
}
else
{
return null;
}
}

The problem is in the following line
 
XmlNode
[] contentNode = request.Headers.GetHeader<XmlNode[]>(headerIndex, serializer);

where I get an InvalidCastException.
Does anybody knows what am I doing wrong? Any ideas will be appreciated.

Answers (2)