Facebook C# SDK LoginButton state management -


i have implemented facebook c# sdk client loginbutton in windows 8 app , have working fine in mode similar provided sample app. issue in beyond sort of "hello world" sample app, login button needs able synchronize state in order useful.

the obvious issue when navigating away page login button, later navigating page, login button has no state @ (it has no session, no user, doesn't know you're logged in, etc). more subtle version of problem in real app typically don't want force user authenticate facebook unless required (so if have valid token previous session of app, user considered logged in , don't go prompting them log in again when app starts). since there no way communicate state login button control, , control doesn't seem serialize/manage own state, can't of this.

i'm new windows 8 programming, maybe i'm missing magic connection make work (i see events indicate when state changes , classes serialize state, don't see way state loginbutton, or otherwise direct login button serialize state). , loginbutton class nailed down point there doesn't seem way hammer state in - related session state read-only/private.

the fact login button (and rest of controls) not used in non-trivial sdk example (scrumptious) not inspire confidence in real-world readiness of these controls.

ok, looks i'm going answer own question ;)

the loginbutton uses facebooksessionclient internally login/logout, , facebooksessionclient in fact serialize session state using appropriate facebooksessioncacheprovider. far, good.

the trick set existing session state button when page loaded. convinced not possible, loginbutton properties currentsession , currentuser read-only. after further review, found can set these values via dependency properties using setvalue.

i track session , user state loginbutton using appropriate event handlers, follows:

    private void onsessionstatechanged(object sender, facebook.client.controls.sessionstatechangedeventargs e)     {         if (e.sessionstate == facebook.client.controls.facebooksessionstate.opened)         {             app.currentsession = this.loginbutton.currentsession;         }         else if (e.sessionstate == facebook.client.controls.facebooksessionstate.closed)         {             app.currentsession = null;              // control signals when user info set (handled in onuserinfochanged below), not when             // cleared (probably bug), clear our reference here when session ends.             //             app.currentuser = null;         }     }      private void onuserinfochanged(object sender, userinfochangedeventargs e)     {         app.currentuser = this.loginbutton.currentuser;     } 

on page load, session state cache (so works when launching app, when resuming app, , when navigating page page) , restore loginbutton state appropriate using setvalue. have added logic automatically re-auth if user logged in, has expired token (happens on launch due use of short-lived tokens). have logic reload currentuser when needed. result of login button has correct state in 3 cases.

    private async void navigationhelper_loadstate(object sender, loadstateeventargs e)     {         this.loginbutton.applicationid = constants.facebookappid;          app.currentsession = facebooksessioncacheprovider.current.getsessiondata();         if ((app.currentsession != null) && (app.currentsession.expires <= datetime.utcnow))         {             // user logged in, session expired.  log them in again...             app.currentsession = await app.facebooksessionclient.loginasync(constants.facebookpermissions);         }          if (app.currentsession != null)         {             this.loginbutton.setvalue(loginbutton.currentsessionproperty, app.currentsession);             if (app.currentuser == null)             {                 facebookclient client = new facebookclient(app.currentsession.accesstoken);                 dynamic result = await client.gettaskasync("me");                 app.currentuser = new graphuser(result);             }         }          if (app.currentuser != null)         {             this.loginbutton.setvalue(loginbutton.currentuserproperty, app.currentuser);         }     } 

i think right solution loginbutton session state using cache provider , initialize appropriately (at least in case session valid/unexpired) work "magically" without of these gymnastics.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -