Authorization Header from my console not received in Restful WCF webservice

407 views Asked by At

I'm actually new in C# and i'm currently building a simple basic Restful WCF webservice which using custom header authorization to proceed request

 public void validateHeader(string requestBody)
    {
        try
        {
            IncomingWebRequestContext WebReq = WebOperationContext.Current.IncomingRequest;
            string requestedURL = WebReq.UriTemplateMatch.RequestUri.OriginalString;
            string clientHeader = WebReq.Headers["Authorization"];
            if (clientHeader.Substring(0, 3) != "amx")
            {
                BuildUnauthorizedError("Unknown header");
            }

Then i debugged it using Postman , with Authorization Header value added and it works as it should enter image description here

But the problem is , when i try to debug the WCF using this console app :

try
        {

            string requestURL = "http://localhost:62146/ORIListenerService.svc/GetDataStatusPembayarans";
            var request = (HttpWebRequest)WebRequest.Create(requestURL);
            request.Method = "GET";
            request.ContentType = "application/json";
            request.PreAuthenticate = true;
            request.Headers.Add("Authorization","amx 12345");

            var response = (HttpWebResponse)request.GetResponse();

the Authorization header is not even received on the WCF

enter image description here

I also tried to fill the header value with random text , and its all received on WCF except the Authorization .. Am i missing something or i just did it all wrong ?

Thanks in advance

2

There are 2 answers

0
RLHakim On BEST ANSWER

i found the solution, it turns out i missed the trailing / in my requestURL.

My UriTemplate in [OperationContract] is like this

UriTemplate = "/GetDataStatusPembayarans/"

but i call URL http://localhost:62146/GetDataStatusPembayarans

1
Adila Shauket On

Try adding "Accept" header parameter in request, thanks.