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??
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:
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