简体   繁体   中英

Facebook SDK for .NET Login Button, logout code

I've used the Login Button Control in the Facebook SDK for .NET in an app and I need to change the Status/state of the login button control from "Logout" to "Login" ie, I need to logout the user manually through code.

How would that be possible??

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM