cookies - How to access protected OData resources from c# application using Microsofts OData v4 Client T4 Code Generator -


i have website exposes odatas $metadata further requires request authenticated (using cookie).

i want access console app, not browser.

i using microsofts odata v4 client code generator.

1) create wrapper around provided container created odata client code generator.

2) log in , cookie need authentication

3) add hook request builder, can apply cookies @ request time. app, needed cookie name .aspnet.applicationcookie

here full working example. can instantiate container user , password needed defined @ bottom. must match whatever controller @ login api expecting.

using nito.asyncex; using system; using system.linq; using system.net; using system.net.http; using system.net.http.headers; using system.threading.tasks;  namespace myappodataodataservice.default {     public class myappodatacontainer : container     {         public cookie[] _myappodataauthcookie;         public string cookieauthname = ".aspnet.applicationcookie";         private string baseurl = "https://theappwwebsite.co.jp/";         public myappodatacontainer(myappodatalogininfo logininfo ) :             base(new uri("https://theappwwebsite.co.jp/odata/"))         {              // init authorization             _myappodataauthcookie = asynccontext.run(() => authenticateuser(logininfo));              if (_myappodataauthcookie == null) throw new unauthorizedaccessexception();               this.buildingrequest += addcookie;          }          private void addcookie(object sender, microsoft.odata.client.buildingrequesteventargs e)         {             e.headers.add("cookie", cookieauthname+"=" + _myappodataauthcookie.first(c=>c.name == cookieauthname).value);         }           private async task<cookie[]> authenticateuser(myappodatalogininfo logininfo)         {             cookiecontainer cookies = new cookiecontainer();             httpclienthandler handler = new httpclienthandler();             handler.cookiecontainer = cookies;             httpclient client = new httpclient(handler);              client.baseaddress = new uri(baseurl);             client.defaultrequestheaders.accept.clear();             client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json"));              uri uri = new uri(baseurl + "/login/login");             httpresponsemessage response = await client.postasjsonasync(uri, logininfo);             response.ensuresuccessstatuscode();              // return uri of created resource.             return  cookies.getcookies(uri).cast<cookie>().toarray();           }       }      public class myappodatalogininfo     {         public string username { get; set; }          public string password { get; set; }      } } 

thanks:

how apply cookie:

creating client code:

cookiecontainer explaination:

post operation idea here - authorizing - having use postasjsonasync


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -