Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use AuthHeader as you did before switching to WCF. Maybe it will be more convinient for you, cause the princples will remain the same. The bad thing i see in this solution is a plain text password transfer. Anyway, it's just another option and you can encrypt/decrypt the password somehow.</p> <p>In this case you should implement your own your IDispatchMessageInspector &amp; IClientMessageInspector, like</p> <pre><code>[AttributeUsage(AttributeTargets.Class)] public class CredentialsExtractorBehaviorAttribute : Attribute, IContractBehavior, IDispatchMessageInspector { #region IContractBehavior implementation. public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime) { dispatchRuntime.MessageInspectors.Add(this); } ... empty interface methods impl skipped ... #endregion #region IDispatchMessageInspector implementation. public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { int i = request.Headers.FindHeader("username", "sec"); if (-1 != i) { string username = request.Headers.GetHeader&lt;string&gt;("username", "sec"); ... do smth ... } return null; } public void BeforeSendReply(ref Message reply, object correlationState) { return; } #endregion } </code></pre> <p>In a sample i placed to header only username, but you can implement your a class containing username and password and use it instead of string. On the client:</p> <pre><code>internal class CredentialsInserter : IContractBehavior, IClientMessageInspector { private string m_username; public CredentialsInserter(string username) { m_username = username; } #region IContractBehavior implementation. ... empty interface methods impl skipped ... public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime) { clientRuntime.MessageInspectors.Add(this); } #endregion #region IClientMessageInspector implementation. public object BeforeSendRequest(ref Message request, IClientChannel channel) { MessageHeader&lt;string&gt; mh = new MessageHeader&lt;string&gt;(m_username); request.Headers.Add(mh.GetUntypedHeader("username", "sec")); return null; } public void AfterReceiveReply(ref Message reply, object correlationState) { return; } #endregion } </code></pre> <p>Then you should place attribute CredentialsExtractorBehaviorAttribute on your service implementation class.</p> <pre><code>[CredentialsExtractorBehavior] public class DummyService : IDummyService { ... impl ... } </code></pre> <p>And on the client side you should do the following:</p> <pre><code> using (DummyServiceClient c = new DummyServiceClient("TcpEndpoint")) { c.ChannelFactory.Endpoint.Contract.Behaviors.Add( new CredentialsInserter("_username_")); c.DummyMethod(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload