检查Facebook登录状态,并使用Facebook的C#SDK获取会话的详细信息 [英] Check Facebook login status and get session details using Facebook C# SDK
问题描述
在前端,我已经初始化使用FB和登录:
window.fbAsyncInit =功能(){
FB.init({
APPID:@facebookAppId,//应用程序ID
状态:真正的,//检查登录状态
饼干:真,//启用Cookie允许服务器访问会话
XFBML:真,//解析XFBML
的OAuth:真正的
});
//额外的初始化代码在这里
FB.Event.subscribe('auth.login',函数(){
了window.location =/facebookPostAuth.aspx;
});
FB.Event.subscribe('auth.logout',函数(响应){
了window.location ='/注销';
});
//哈克修复http://bugs.developers.facebook.net/show_bug.cgi?id=20168为IE7 / 8/9
FB.UIServer.setLoadedNode =功能(的a,b){FB.UIServer._loadedNodes [a.id] = b; };
};
//加载SDK异步
(函数(D){
变种JS,ID ='Facebook的jssdk';如果(d.getElementById(ID)){返回;}
JS = d.createElement('脚本'); js.id = ID; js.async = TRUE;
js.src =//connect.facebook.net/en_US/all。 JS;
d.getElementsByTagName(头)[0] .appendChild(JS);
}(文件));
在后端,我怎么检查是否有现有的使用Facebook的C#SDK一个登录会话?
这是不亚于我有,但我不知道如何在APP ID /密传以获得更多的信息(如电子邮件,其中我在登录范围inluded):
VAR的客户=新FacebookClient();
动力箱= client.Get(我);
串的firstName = me.first_name;
字符串的lastName = me.last_name;
字符串email = me.email;
VAR客户端=新FacebookClient(APPID,appSecret);
或
VAR的客户=新FacebookClient(的accessToken);
您可以使用JavaScript SDK抢访问令牌的客户端,并将其传递到服务器一个隐藏的表单字段(常规或AJAX),或者C#SDK暴露出它发现在cookie中和/或通过FacebookWebContext.Current.AccessToken签名的请求。在过去,我已经发现了一些时序问题,其中Facebook的没有更新及时新的访问令牌/签名的请求,这就是为什么我把它在Javascript并将其张贴到服务器的cookie。
On the front-end, I've initialised FB and login using:
window.fbAsyncInit = function () {
FB.init({
appId: @facebookAppId, // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true, // parse XFBML
oauth: true
});
// Additional initialization code here
FB.Event.subscribe('auth.login', function () {
window.location = "/facebookPostAuth.aspx";
});
FB.Event.subscribe('auth.logout', function (response) {
window.location = '/logout';
});
// Hack to fix http://bugs.developers.facebook.net/show_bug.cgi?id=20168 for IE7/8/9
FB.UIServer.setLoadedNode = function (a, b) { FB.UIServer._loadedNodes[a.id] = b; };
};
// Load the SDK Asynchronously
(function (d) {
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; }
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
} (document));
On the back-end, how do I check if there is a login session existing using Facebook C# SDK?
This is as much as I've got, but I'm not sure how to pass in the APP ID / Secret to get more information (such as email which I've inluded in login scope):
var client = new FacebookClient();
dynamic me = client.Get("me");
string firstName = me.first_name;
string lastName = me.last_name;
string email = me.email;
var client = new FacebookClient(appID, appSecret);
or
var client = new FacebookClient(accessToken);
You can grab the access token client-side with the javascript SDK and pass it up to the server in a hidden form field (conventional or ajax), or the C# SDK exposes what it finds in the cookie and/or signed request via FacebookWebContext.Current.AccessToken. In the past I have found some timing issues where Facebook did not update the cookie with a new access token/signed request in a timely fashion, which is why I get it in Javascript and post it to the server.
这篇关于检查Facebook登录状态,并使用Facebook的C#SDK获取会话的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!