Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WCF service running as a windows service. It is running with a specific user as a security context (username@domain.com) . There is a method in it which calls a REST API web service. I wanted to pass credentials of WCF service to REST request. I tried below code but it is not working. All I am getting is Unauthorized (401) error from REST web service.
C#
Uri uri = new Uri(objM3API.URL);
ICredentials credentials = CredentialCache.DefaultCredentials;
NetworkCredential credential = credentials.GetCredential(uri, "Basic");

request.Credentials = credential;
request.Accept = "application/json";
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    if (response.StatusCode == HttpStatusCode.OK)
    {



So far I tried changing Authentication type when creating NetworkCredential object to "Negotiate" and "NTLM" but that did not work either. I don't want to create NetworkCredential object passing user name and password ( Request from company to use security context of the service rather than passing value directly ) .

Any help will be greatly appreciated
Posted
Comments
ZurdoDev 14-Mar-14 16:58pm    
What kind of security/authentication does the service have? If it is Forms Authentication, for example, you'll have to do it differently.
virang_21 14-Mar-14 19:34pm    
The problem is those REST eeb services are exposed by corporate ERP and there is no documentation on how to use them. You may be right that it may be using forms auth..how to do it if it is using forms auth ?
virang_21 14-Mar-14 19:39pm    
The code that i posted was workingbut they made some changes to make our test ERP environment same as production and it stoped working. It is good that i find out about it before system went to prod but now i am stumped on how to authenticate user when calling web service.
ZurdoDev 14-Mar-14 19:42pm    
You should ask them what they changed then.
virang_21 14-Mar-14 21:55pm    
It is using Basic Authentication to validate credential. I can confirm this because when I execute REST API url directly in browser it prompts for username/password dialog. Also using Soap UI tool it is showing WWW-Authenticate Basic Realm="TEST" as a part of response header. My fear is that somehow when creating NetworkCredential object the way I did is not passing credential info to the request. Any suggestion ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900