Simple Odata Client to consume Odata with Authentication not working

2.9k Views Asked by At

I m new to Simple.Odata.client. I had a problem to access the Odata Service with below code. The below code return null. but Postman return with result.

  1. suspected Problem : How to pass a url string with '1000' &format=json
  2. Is the below Simple odata client setup correctly?
  3. There is no UrlBase in Simple Odata client, but there is BAseUri

  4. Is this ODataClientSettings working??

    var settings = new Simple.OData.Client.ODataClientSettings();

    settings.BaseUri = new Uri("https://..../UoM?$filter=wer eg '1000' &format=json");

    settings.Credentials = new NetworkCredential("user1", "usrpwd");
    var client = new ODataClient(settings);

please help

Thanks

1

There are 1 best solutions below

0
On

This worked for me

var credentials = new NetworkCredential(userName, password); //you can use the override with the domain too.
var settings = new ODataClientSettings(baseUrl, credentials) //baseUrl is a string.
        {
            IgnoreResourceNotFoundException = true,
            OnTrace = (x, y) => Debug.WriteLine(x, y),
            PayloadFormat = ODataPayloadFormat.Json, //here is where you specify the format
            IgnoreUnmappedProperties = true,
            RenewHttpConnection = true,
            TraceFilter = ODataTrace.All,
            PreferredUpdateMethod = ODataUpdateMethod.Merge
        };
var client = new ODataClient(settings);

Your baseUrl should not contain all those OData tags but the endpoint of your service like https://myservice.mysite.com/api.svc. Then as you use the Simple.OData.Client the resource url will be automatically completed.

Please, take a look at the OData standard to figure out how it works and see the Simple.OData.Client repo's examples to better understand how to use it.

To better understand how to use the Windows Authentication you can check Authentication and Authorization with Windows Accounts and how to access website with Windows credential

Hope this help.