Question

Hello I am using this example to login on Facebook using Facebook .net sdk.Eveything works now I have to save the login button state i.e change the button state i.e from log in to log out and also if already connected the display the FB user's name. How can I achieve this?

Was it helpful?

Solution

If saving the state means you want to save information like access_token, profile details etc. Suppose you want to store the access_token only, you can save it to isolated storage:

     private void OnSessionStateChanged(object sender, Facebook.Client.Controls.SessionStateChangedEventArgs e)
            {
               if (e.SessionState == Facebook.Client.Controls.FacebookSessionState.Opened)
                {
                 string fb_access_token = ((Facebook.Client.Controls.LoginButton)sender).CurrentSession.AccessToken;
                 IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                 settings["fb_token"] = fb_access_token ;
                 settings.Save();
                }
             }

And in your application you can add a logout button, on its click you can delete access_token from isolated storage.

private void onLogOutClick(object sender, EventArgs e)
        {
           IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
           settings.Remove("access_token");                  
           settings.Save();
         }

On application launch you can check if access_token exist, you can navigate to another page. If not you can redirect to login page.

  private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        if (IsolatedStorageSettings.ApplicationSettings.Contains("fb_token"))
        {                
    // Use this access token in application for fetching other user informations
            Uri nUri = new Uri("/Page1.xaml", UriKind.Relative);
            RootFrame.Navigate(nUri);       
        }
        else
        {
            Uri nUri = new Uri("/Login.xaml", UriKind.Relative);
            RootFrame.Navigate(nUri);
        }           
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top