c# - How to add security header to soap message in WCF, -


i trying add soap security header username token in wcf, sorry have posted trails in previous post, got more method , added here may work. getting exception

"cannot call 'writeendelement' while depth '0'."

   using system; using system.collections.generic; using system.linq; using system.runtime.serialization; using system.security.cryptography; using system.servicemodel; using system.servicemodel.channels; using system.servicemodel.description; using system.servicemodel.dispatcher; using system.servicemodel.web; using system.text; using system.xml; using microsoft.web.services3.design; using microsoft.web.services3; //using customassertion.servicereference1;  namespace test {      public class inspectorbehavior : iendpointbehavior     {         public clientinspector clientinspector { get; set; }         public inspectorbehavior(clientinspector clientinspector)         {             clientinspector = clientinspector;         }         public void validate(serviceendpoint endpoint)         { }         public void addbindingparameters(serviceendpoint endpoint, bindingparametercollection bindingparameters)         {         }         public void applydispatchbehavior(serviceendpoint endpoint, endpointdispatcher endpointdispatcher)         { }         public void applyclientbehavior(serviceendpoint endpoint, clientruntime clientruntime)         {             if (this.clientinspector == null) throw new invalidoperationexception("caller must supply clientinspector.");             clientruntime.messageinspectors.add(clientinspector);         }     }      public class clientinspector : iclientmessageinspector     {         public messageheader[] headers { get; set; }         public clientinspector(params messageheader[] headers)         {             headers = headers;         }         public object beforesendrequest(ref message request, iclientchannel channel)         {             if (headers != null)             {                 (int = headers.length - 1; >= 0; i--)                     request.headers.insert(0, headers[i]);             }             return request;         }         public void afterreceivereply(ref message reply, object correlationstate)         {         }     }      public class securityheader : messageheader     {         public string systemuser { get; set; }         public string systempassword { get; set; }         public securityheader(string systemuser, string systempassword)         {             systemuser = systemuser;             systempassword = systempassword;         }         public override string name         {             { return "security"; }         }         public override string namespace         {             { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; }         }         protected override void onwritestartheader(xmldictionarywriter writer, messageversion messageversion)         {             writer.writestartelement("wsse", name, namespace);             writer.writexmlnsattribute("wsse", namespace);         }         protected override void onwriteheadercontents(xmldictionarywriter writer, messageversion messageversion)         {             var nonce = new byte[64];             randomnumbergenerator.create().getbytes(nonce);             string created = datetime.now.tostring("yyyy-mm-ddthh:mm:ss.msz");             writer.writestartelement("wsse", "usernametoken", namespace);             writer.writexmlnsattribute("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");             writer.writevalue(systemuser);             //writer.writestartelement("wsse", "username", null);             //writer.writestring(systemuser);             writer.writeendelement();//end username               writer.writestartelement("wsse", "password", namespace);             //writer.writestartelement("wsse", "password", null);             writer.writeattributestring("type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#passworddigest");             writer.writevalue(computepassworddigest(systempassword, nonce, created));             //writer.writestring(computepassworddigest(systempassword, nonce, created));             writer.writeendelement();//end password               writer.writestartelement("wsse", "nonce", null);             writer.writeattributestring("encodingtype", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#base64binary");             writer.writebase64(nonce, 0, nonce.length);             writer.writeendelement();//end nonce               // writer.writestartelement("wsse", "created", null);             // writer.writestring(created);             // writer.writeendelement();//end created              // writer.writeendelement();//end usernametoken              writer.flush();         }         private string computepassworddigest(string secret, byte[] nonceinbytes, string created)         {             byte[] createdinbytes = encoding.utf8.getbytes(created);             byte[] secretinbytes = encoding.utf8.getbytes(secret);             byte[] concatenation = new byte[nonceinbytes.length + createdinbytes.length + secretinbytes.length];             array.copy(nonceinbytes, concatenation, nonceinbytes.length);             array.copy(createdinbytes, 0, concatenation, nonceinbytes.length, createdinbytes.length);             array.copy(secretinbytes, 0, concatenation, (nonceinbytes.length + createdinbytes.length), secretinbytes.length);             return convert.tobase64string(sha1.create().computehash(concatenation));         }     }     public class service1 : iservice1     {          public string security(int a)         {             servicereference1.service1client action = new servicereference1.service1client();             action.endpoint.behaviors.add(new inspectorbehavior(             new clientinspector(new securityheader("username", "password"))));             return action.getdata(8);         }      } } 

kindly suggest other scenarios if need add, waiting reply experts.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -