0

I have this code in C# Windows forms app

string data = "";
var client = new RestClient("http://server:port/ords/xx_portal_dev/oauth/token");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("Cookie", "BCSI-CS-8eb7fa809579930b=1");
request.AddHeader("Content-Length", "29");
request.AddHeader("Accept-Encoding", "gzip, deflate");
request.AddHeader("Host", "server:port");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "*/*");            
request.AddHeader("Authorization", "Basic 3244234wfsdf2342234...");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("undefined", "grant_type=client_credentials", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
data = response.Content.ToString();

it work fine in WCF Service Method, but i´m working with windows form application and i get error when run this code on a buttom, I don´t know what is happening...

I always get error Unauthorized "Access to this resource is protected", but in my WCF service it´s working fine!!!

stuartd
  • 70,509
  • 14
  • 132
  • 163

2 Answers2

0

Taken from here. https://stackoverflow.com/a/14628308/1390548

have you tried this to validate the encoding is correct?

request.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue(
        "Basic", Convert.ToBase64String(
            System.Text.ASCIIEncoding.ASCII.GetBytes(
               $"{yourusername}:{yourpwd}")));
Jawad
  • 11,028
  • 3
  • 24
  • 37
  • Yes my encoding is correct, I have tested the value with the postman client and it works well. – Hugo Lima Rodriguez Dec 06 '19 at 16:19
  • Postman gives you the option to export c# code for the request that works. https://learning.getpostman.com/docs/postman/sending-api-requests/generate-code-snippets/ – Jawad Dec 06 '19 at 16:25
  • Yes, postman use restsharp api, i have install restsharp API in both clients WindowsForms and WCF Service but this code only work well in WCF Client, I need to know what is hapening with my windows app, because i alway get Unauthorized message – Hugo Lima Rodriguez Dec 06 '19 at 16:41
  • I meant, export the postman request from postman and copy that code in C# to test. – Jawad Dec 06 '19 at 16:42
0

The above code snippets are working on the client-sides. It is a process of calling the service. But you say that it works well in your WCF service. is it a client application? it should be a server application which creates the WCF service.
Besides, an Unauthorized request usually due to the wrong credential, please check if the below header is valid.

request.AddHeader("Authorization", "Basic 3244234wfsdf2342234...");

Finally, How the client calls depend on the configuration of the service. Please post the complete the server configuration, I will try to write you an example of how to call.
Feel free to let me know if the problem still exists.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
  • Hello, i´m testing Rest oracle cloud service, I need to build windows application client to consume the service. In my testing procees i have create both client (win and WCF) only for evaluate oracle service. My code work fine in wcf client, but the same code doesn´t work in windows application. The header an encoding is correct because i have test this with my WCF service Client and even work well with the Postman client application. Do you have any idea what is hapeening? – Hugo Lima Rodriguez Dec 06 '19 at 16:13
  • Are these two applications running in the same machine? On the server-side, there might be a difference in handling the request from different machines – Abraham Qian Dec 09 '19 at 03:32
  • No, both applications run on the same client-side machine. WCF application lifts an IIS express instance, while the desktop application does not. It is the only difference. – Hugo Lima Rodriguez Dec 10 '19 at 22:17
  • What do you mean that WCF application lifts an IIS express instance? Also, I could not understand how do you make a successful call to the service in WCF client application. what is your WCF client application? As far as I know, WCF client application call the service usually with a client proxy. but the above invocation is directly sending an HTTP Post Request. This is totally different. Can you call the service properly in a console application? – Abraham Qian Dec 11 '19 at 05:39