Note that there are some explanatory texts on larger screens.

plurals
  1. POsetRequestHeader not adding request header to wcf call
    primarykey
    data
    text
    <p>I am trying to make a call to wcf function using ajax as below:</p> <pre><code>$.ajax({ url:http://localhost:64121/Test.svc/json/GetNumber?X='+ var1 + '&amp;callback=?', dataType: 'json', beforeSend : function(xhr) { xhr.setRequestHeader("Authorization", "BasicAuthTest"); }, success: function(trackingData) { alert("success"); } }); </code></pre> <p>WCF code is beaking and is receving the request</p> <pre><code>public class CustomUserNameValidatorBasic : ServiceAuthorizationManager { protected override bool CheckAccessCore(OperationContext operationContext) { try { var msg = operationContext.RequestContext.RequestMessage; // If user requests standart help-page then ignore authentication check. if (msg.Properties.ContainsKey("HttpOperationName") &amp;&amp; msg.Properties["HttpOperationName"].ToString() == "HelpPageInvoke") { return base.CheckAccessCore(operationContext); } var httpRequestHeaders = ((HttpRequestMessageProperty) msg.Properties[HttpRequestMessageProperty.Name]).Headers; // Is Authorization-header contained in http-headers? if (!httpRequestHeaders.AllKeys.Contains(HttpRequestHeader.Authorization.ToString())) { //code returns here return false; } return false; } catch (Exception e) { return false; } } } </code></pre> <p>As shown "Authorization" header is not included in req header array</p> <p><img src="https://i.stack.imgur.com/lxVsO.png" alt="enter image description here"></p> <p>In Firebug Under Request Headers for ajax reqeuest:</p> <blockquote> <p>Request Headersview source Accept <em>/</em> Accept-Encoding gzip, deflate<br> Accept-Language en-US,en;q=0.5 Cache-Control max-age=0<br> Connection keep-alive Host test.proxyU.com If-Modified-Since Wed, 21 Mar 2012 19:46:56 GMT If-None-Match "e0818-17278-4bbc60dc86c00"<br> User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0)<br> Gecko/20100101 Firefox/16.0</p> </blockquote> <p>Update: Considering Answer below I have tried 3 Methods as below none of them adds the header. All call wcf correctly but `Authorization' header is just not in the request. And if I watch the http request the header looks very much like the screen shot I posted above</p> <p><strong>Method 1</strong></p> <pre><code>$.ajax({ url: "http://localhost:64121/Test.svc/json/GetNumber?X='+ var1 + '&amp;callback=?'", beforeSend: function(xhr){xhr.setRequestHeader("Authorization", "BasicAuthTest");}, success: function(trackingData) { alert("success"); } }); </code></pre> <p><strong>Method 2</strong></p> <pre><code>$.ajax({ url: "http://localhost:64121/Test.svc/json/GetNumber?X='+ var1 + '&amp;callback=?'", headers: {"Authorization": "BasicAuthTest"}, success: function(trackingData) { alert("success"); } }); </code></pre> <p><strong>Method 3</strong></p> <pre><code>$.ajax({ url: "http://localhost:64121/Test.svc/json/GetNumber?X='+ var1 + '&amp;callback=?'", beforeSend : function(xhr, settings) { $.extend(settings, { headers : { "Authorization" : "BasicAuthTest" } }); }, success: function(trackingData) { alert("success"); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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