Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've also had this problem... When you define NetworkCredentials it does not get sent in the HTTP header, and thus no 'Authorization' header...</p> <p>What I did and it worked for me was:</p> <p>I created a class that implements IClientMessageInspector and implement the BeforeSendRequest method:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel.Dispatcher; using System.ServiceModel.Channels; namespace BasicAuth { public sealed class MessageHttpHeaderInspector : IClientMessageInspector { private string userName; private string password; #region Constructor public MessageHttpHeaderInspector(string userName, string password) { this.userName = userName; this.password = password; } #endregion #region IClientMessageInspector Members public void AfterReceiveReply(ref Message reply, object correlationState) { //throw new NotImplementedException(); } public object BeforeSendRequest(ref Message request, System.ServiceModel.IClientChannel channel) { HttpRequestMessageProperty httpRequest; if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name)) { httpRequest = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty; } else { httpRequest = new HttpRequestMessageProperty(); request.Properties.Add(HttpRequestMessageProperty.Name, httpRequest); } if (httpRequest != null) { string credentials = this.CreateBasicAuthenticationCredentials(this.userName, this.password); httpRequest.Headers.Add(System.Net.HttpRequestHeader.Authorization, credentials); } return request; } #endregion #region Private Worker Methods private string CreateBasicAuthenticationCredentials(string userName, string password) { string returnValue = string.Empty; string base64UsernamePassword = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", userName, password))); returnValue = String.Format("Basic {0}", base64UsernamePassword); return returnValue; } #endregion } } </code></pre> <p>Then I created a class that implements IEndpointBehavior and implements the ApplyClientBehavior method:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Description; using System.ServiceModel.Dispatcher; namespace BasicAuth { public sealed class CustomEndpointCallBehavior : IEndpointBehavior { private string userName; private string password; #region Constructor public CustomEndpointCallBehavior(string userName, string password) { this.userName = userName; this.password = password; } #endregion #region IEndpointBehavior Members public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { //throw new NotImplementedException(); } public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { clientRuntime.MessageInspectors.Add(new MessageHttpHeaderInspector(this.userName, this.password)); } public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { //throw new NotImplementedException(); } public void Validate(ServiceEndpoint endpoint) { //throw new NotImplementedException(); } #endregion } } </code></pre> <p>Then you need to apply this behavior to the proxy:</p> <pre><code>mywebservice.Endpoint.Behaviors.Add(new CustomEndpointCallBehavior("User1", "Pass1")); </code></pre> <p>Have a look at the following:</p> <ul> <li><a href="http://dkochnev.blogspot.com/2011/05/framework-40-wcf-basic-authentication.html" rel="nofollow">http://dkochnev.blogspot.com/2011/05/framework-40-wcf-basic-authentication.html</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.iendpointbehavior.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.servicemodel.description.iendpointbehavior.aspx</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageinspector.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageinspector.aspx</a></li> </ul> <p>This should do the trick... Hope it works for you!</p>
 

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