Facebook SDK for .NET Login Button, logout code

前提是你 提交于 2019-12-04 16:55:46

Use FacebookClient.Logout to generate the logout url.

This is just a sample.

private void btnLogout_Click(object sender, EventArgs e)
{
    var fb = new FacebookClient();

    var logoutUrl = fb.GetLogoutUrl(new
                                        {
                                            next =    "https://www.facebook.com/connect/login_success.html",
                                            access_token = _accessToken
                                        });
    var webBrowser = new WebBrowser();
    webBrowser.Navigated += (o, args) =>
                                {
                                    if (args.Url.AbsoluteUri == "https://www.facebook.com/connect/login_success.html")
                                        Close();
                                };

    webBrowser.Navigate(logoutUrl.AbsoluteUri);
}

Make sure to persist the access token somewhere when you login so that you can use it to the logout too.

The user is not asked for his credentials because the Facebook authentication cookie is still present in the WebBrowser control.

So to completely logout the user from Facebook, you need to clear the WebBrowser cookies.

Unfortunately, there is no easy way for erasing cookies on Windows Phone 7.

On Windows Phone 8 you just need to call ClearCookiesAsync Method

await new WebBrowser().ClearCookiesAsync();

Here is a tutorial that makes use of it:

Integrate Facebook to Your Windows Phone Application

Alex

I've spent many hours trawling to get this. Many thanks Kulasangar

I modified your code slightly but it works:

var identity = AuthenticationManager.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie);
var accessToken = identity.FindFirstValue("FacebookAccessToken");
var fb = new FacebookClient(accessToken);

var logoutUrl = fb.GetLogoutUrl(new
                {
                    next = "https://www.facebook.com/connect/login_success.html",
                    access_token = accessToken
                });
Response.Redirect(logoutUrl.AbsoluteUri);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!