Biljanka
Biljanka

Reputation: 61

FB Logout with C# sdk

I'm logged in to my app using my FB credentials. In the end I do a logout and remove my session variables. I'm logged out from application, but the FB session remains open although I do a post to the FB logout page with the post like in the code:

if (Session["FBAccessToken"] != null){

  var fb = new Facebook.FacebookClient();
  string accessToken = Session["FBAccessToken"] as string;
  //var logoutUrl = fb.GetLogoutUrl(new { access_token = accessToken, next = "https://www.facebook.com/", });
  var logoutUrl = fb.GetLogoutUrl(new {  next = "https://www.facebook.com/", });

  fb.Post(logoutUrl.AbsoluteUri, new { access_token = accessToken });
  Session.RemoveAll();
}

I've tried both: logoutUrl generated with and without access token parameter, neither worked for me.

Upvotes: 6

Views: 4431

Answers (2)

nanonerd
nanonerd

Reputation: 1984

@prabir has the answer. Here is a complete answer after I tweaked it for my MVC app. Just replace "localhost:51042/" with whatever URL is appropriate for your app.

This actually fills a hole in the tutorial: http://www.asp.net/mvc/overview/getting-started/using-oauth-providers-with-mvc

public ActionResult LogOff()
    {
        WebSecurity.Logout();

        if (Session["facebooktoken"] != null)
        {
            var fb = new Facebook.FacebookClient();
            var logoutUrl = fb.GetLogoutUrl(new { access_token = Session["facebooktoken"], next = "http://localhost:51042/" });

            Response.Redirect(logoutUrl.AbsoluteUri);
            Session.RemoveAll();
        } 

        return RedirectToAction("Index", "Home");
    }

Upvotes: 0

prabir
prabir

Reputation: 7794

There has been changes on facebook logout since my last blog post. Here is the way to logout.

var fb = new FacebookClient();
var logoutUrl = fb.GetLogoutUrl(new {access_token = "...", next = "...." });

// redirect to logoutUrl.AbsoluteUri

next url cannot be any arbitrary url. I has to be the one that is part of the site url which you used to retrieve the access token.

Upvotes: 2

Related Questions