使用C#进行Facebook登录和注销

问题描述:

亲爱的朋友,

我必须在我的网站中使用facebook登录/注销,但无法以正确的方式实施.
我已经在Google中搜索了多个代码段,但对我没有用,或者我丢失了某些内容.

请帮助我提供代码段或最佳解决方案,以便我可以实现它.

Dear friend,

I have to use facebook Login/Logout in my website, but not able to implement it in right way.
I have searched several code snippet in google but not worked for me or I am missing something.

Please help me with code snippet or best solution ,so that I can implement it.

<html> 
<head> 
<title>Facebook Login Authentication Example</title> 
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <script> // Load the SDK Asynchronously (function(d) { var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) { return; } js = d.createElement('script'); 
js.id = id;
 js.async = true;
 js.src = "//connect.facebook.net/en_US/all.js";
 ref.parentNode.insertBefore(js, ref);
 } (document));
 // Init the SDK upon load window.fbAsyncInit = function() 
{ 
FB.init({ appId: '198191300293654', 
// App ID channelUrl:
 '//' + window.location.hostname + '/channel', 
// Path to your Channel File status:
 true, // check login status cookie:
 true, 
// enable cookies to allow the server to access the session xfbml:
 true 
// parse XFBML });
 // listen for and handle auth.
statusChange events FB.
Event.subscribe('auth.statusChange', function(response) { if (response.authResponse) { 
// user has auth'd your app and is logged into Facebook
 FB.api('/me', function(me) { if (me.name) { document.getElementById('auth-displayname').innerHTML = me.name; 
} }) document.getElementById('auth-loggedout').style.display = 'none'; 
document.getElementById('auth-loggedin').style.display = 'block'; 
} 
else 
{ 
// user has not auth'd your app, or is not logged into Facebook 
document.getElementById('auth-loggedout').style.display = 'block';
 document.getElementById('auth-loggedin').style.display = 'none';
 } });
 $("#auth-logoutlink").click(function() 
{ 
FB.logout(function() 
{ window.location.reload();
 }); }); } </script>


Facebook登录身份验证示例


Facebook Login Authentication Example

<div id="auth-status"> 
<div id="auth-loggedout"> 
<div class="fb-login-button" autologoutlink="true" scope="email,user_checkins">Login with Facebook</div>
</div> 
<div id="auth-loggedin" style="display: none"> Hi, <span id="auth-displayname"></span>(logout) </div> </div> </body> </html>



预先感谢.



Thanks in advance.

(" ).click(函数() { FB.logout(函数() {窗口.位置 .reload(); }); }); } < / 脚本 >
("#auth-logoutlink").click(function() { FB.logout(function() { window.location.reload(); }); }); } </script>


Facebook登录身份验证示例


Facebook Login Authentication Example

<div id="auth-status"> 
<div id="auth-loggedout"> 
<div class="fb-login-button" autologoutlink="true" scope="email,user_checkins">Login with Facebook</div>
</div> 
<div id="auth-loggedin" style="display: none"> Hi, <span id="auth-displayname"></span>(logout) </div> </div> </body> </html>



预先感谢.



Thanks in advance.


使用此脚本注销Facebook.它可以在我的网站上运行

Use this script for facebook logout. It works on my site

<form>
<div id="fb-root">
<script language="javascript">
window.onload=function()
{
    // initialize the library with your Facebook API key
    FB.init({ apiKey: 'b65c1efa72f570xxxxxxxxxxxxxxxxx' });

    //Fetch the status so that we can log out.
    //You must have the login status before you can logout,
    //and if you authenticated via oAuth (server side), this is necessary.
    //If you logged in via the JavaScript SDK, you can simply call FB.logout()
    //once the login status is fetched, call handleSessionResponse
    FB.getLoginStatus(handleSessionResponse);
}

//handle a session response from any of the auth related calls
function handleSessionResponse(response) {
    //if we dont have a session (which means the user has been logged out, redirect the user)
    if (!response.session) {
        window.location = "/mysite/Login.aspx";
        return;
    }

    //if we do have a non-null response.session, call FB.logout(),
    //the JS method will log the user out of Facebook and remove any authorization cookies
    FB.logout(handleSessionResponse);
}
</script>
</div></form>