kubanek
kubanek

Reputation: 21

OData, DataServiceClientException: Unauthorized

I'm developing (as a totally no-C#-guy) a web app which communicates with an external data source over OData (server B, some windows 2k12 machine). The web app runs (is about to run) on an IIS (server A, another windows 2k12 machine), the OData source is a Dynamics NAV 2015 service (the first mentioned windows 2k12 machine, server B). I'm developing it in VS2013 and if I'm running this locally (meaning: without publishing it, only running it within the local express IIS) it works without any problems at all. But as soon as I publish it to the target IIS (server A) I'm getting:

An error occurred while processing this request. ---> >System.Data.Services.Client.DataServiceClientException: Unauthorized at System.Data.Services.Client.QueryResult.ExecuteQuery() at System.Data.Services.Client.DataServiceRequest.Execute[TElement]>(DataServiceContext context, QueryComponents queryComponents) --- End of inner exception stack trace ---

This is esentially the piece of code which deals with the OData call:

ODataOrders.NAV odata = new ODataOrders.NAV(new Uri(serviceUri));
System.Net.NetworkCredential nc = new 
System.Net.NetworkCredential(_oDataUsername, _oDataUserPassword, _oDataDomain);
odata.Credentials = nc;
DataServiceQuery<ODataOrders.Orders> query = 
odata.CreateQuery<ODataOrders.Orders>("Orders");
orderList = query.Execute().ToList();  // "Unauthorized" is being thrown here

It looks like the credentials are being sent/accepted if running from local IIS. And if running from the target (production) server A, the credentials are being somehow lost/overwritten? I really don't know now... Server A and Server B are in the same domain. My development environment isn't - I can even connect from home through VPN to my work domain and launch my local IIS and still be able to get that request done (the request goes through my local IIS and reaches the Dynamics NAV service so the data is being actually fetched). Any help would be appreciated... EDIT It's Dynamics NAV 2015 CU11.

Upvotes: 1

Views: 3095

Answers (1)

kubanek
kubanek

Reputation: 21

After some serious hours of reading, programming and trying I've stumbled upon a post here which gave me an idea. And guess what? It worked - somehow...

Instead of:

    System.Net.NetworkCredential(_oDataUsername, _oDataUserPassword, _oDataDomain);

I submitted only:

    System.Net.NetworkCredential(_oDataUsername, _oDataUserPassword);

I turns out somehow the domain caused the call not to be authorized (401 error).

Upvotes: 0

Related Questions