Facebook SDK for .NET Login Button, logout code

*爱你&永不变心* 提交于 2019-12-06 09:17:04

问题


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" i.e., I need to logout the user manually through code.

How would that be possible??


回答1:


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.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/23951666/facebook-sdk-for-net-login-button-logout-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!