3

I have been looking through quite a few posts on StackOverflow and on the internet on being able to log a user out of Facebook through an ASP.NET LinkButton.

I have tried implementing solutions from the following posts:

Code

ASPX Page

<asp:LinkButton ID="LogoutButton" CssClass="log-out fb"  OnClick="LogoutButton_Click" runat="server">Logout</asp:LinkButton>

JavaScript

$(".log-out.fb").click(function () {
    FB.logout(function (response) {
        //Logged out
        FB.Auth.setAuthResponse(null, 'unknown');
    });
});

HTML Output

<a id="MainContent_LogoutButton" class="log-out fb" href="javascript:__doPostBack('ctl00$MainContent$LogoutButton','')" style="width: 66px; ">Logout</a>

I definitely know that the jQuery click event is getting fired when debugging via Firebug. The jQuery code works fine when used in conjunction with a standard HTML anchor, so there is no reason for it not to work on a ASP.NET LinkButton.

Any help would be appreciated.

6
  • 1
    There is no such thing as an “ASP.NET LinkButton” in HTML. Please show us the actual HTML code generated by your server-side script, or even better, the URL to an example page to look at it.
    – CBroe
    Aug 3, 2012 at 12:19
  • I've modified my post to say "ASPX" instead of "HTML". :-) I will post the HTML generated by .NET. Aug 3, 2012 at 12:44
  • @CBroe Post updated. From looking at the HTML output, count the href="javascript:__doPostBack('ctl00$MainContent$LogoutButton','')" be causing the problem? Aug 3, 2012 at 12:59
  • From the name of it, it sounds like it is doing some stuff like posting back to the server, so yeah, that could be the problem. If you just want to have an HTML A element, then why don’t you just write one of those in your code anyways, and forget about those asp-button-thing altogether …?
    – CBroe
    Aug 3, 2012 at 14:44
  • 1
    You could just call your code you want to execute server-side in the callback function of FB.logout, by simple pointing to a URL that gets that code executed via location.href = "…"
    – CBroe
    Aug 3, 2012 at 14:53

1 Answer 1

3

Thanks for all your help. But I managed to find a way to log out a user by using the following link:

https://www.facebook.com/logout.php?next=YOUR_URL&access_token=ACCESS_TOKEN

I created a callback page similar to the one from this article. Once I received the "access token", I managed to log the user out.

Here is my code for my callback page:

protected void Page_Load(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(Request["code"]) && !Page.IsPostBack)
    {
        FacebookCallback();
    }
}

private void FacebookCallback()
{
    var client = new RestClient { Authority = "https://graph.facebook.com/oauth/" };
    var request = new RestRequest { Path = "access_token" };

    request.AddParameter("client_id", ConfigurationManager.AppSettings["facebook.appid"]);
    request.AddParameter("redirect_uri", ConfigurationManager.AppSettings["facebook.logout.callbackurl"]);
    request.AddParameter("client_secret", ConfigurationManager.AppSettings["facebook.appsecret"]);
    request.AddParameter("code", Request["code"]);

    RestResponse response = client.Request(request);
    // A little helper to parse the querystrings.
    StringDictionary result = QueryStringHelper.ParseQueryString(response.Content);

    string aToken = result["access_token"];

    LogUserOut(aToken);
}

private void LogUserOut(string sToken)
{
    string url = String.Format("https://www.facebook.com/logout.php?next=http://{0}/Default.aspx&access_token={1}", ConfigurationManager.AppSettings["site.url"], sToken);

    Response.Redirect(url);
}

I hope this helps others if they encounter the same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.